lunes, 23 de noviembre de 2009

Maven

Hola, muchos de los que trabajamos desarrollando j2ee, conocemos ant, como herramienta de automatización del deploy del proyecto, la evolucion de ant es maven, el cual a travez de comenados descarga automaticamente desde los repositorios las librerias y dependencias para la aplicación. Aqui veremos como trabaja Maven 2 para el proyecto appfuse 2.

Primero debemos descargar maven 2.x desde su web, http://maven.apache.org/download.html#Installation para este ejemplo descargue el archivo apache-maven-2.2.1-bin.tar.gz

Luego lo descomprimi en mi directorio de programas, creando el siguiente directorio:

/home/slack/programas/apache-maven-2.2.1

con el siguiente contenido:

slack@zion:~/programas/apache-maven-2.2.1$ ls -la
total 48
drwxr-xr-x 6 slack slack 4096 2009-11-23 12:35 .
drwxr-xr-x 18 slack slack 4096 2009-11-23 12:35 ..
drwxr-xr-x 2 slack slack 4096 2009-11-23 12:35 bin
drwxr-xr-x 2 slack slack 4096 2009-11-23 12:35 boot
drwxrwxrwx 2 slack slack 4096 2009-08-06 15:11 conf
drwxr-xr-x 2 slack slack 4096 2009-11-23 12:35 lib
-rw-r--r-- 1 slack slack 12933 2009-08-06 15:11 LICENSE.txt
-rw-r--r-- 1 slack slack 923 2009-08-06 15:11 NOTICE.txt
-rw-r--r-- 1 slack slack 2532 2009-08-06 15:11 README.txt


Luego que tenemos el directorio de Maven, debemos declararlo en las variables de entorno para poder utilizarlo. como lo descomprimimos en el directorio de usuario modificaremos el profile, en caso contrario el enviromment

Entonces vamos a la consola y escribimos el sigte. comando para editar nuestro profile:

slack@zion:~/programas/apache-maven-2.2.1$ sudo nano /etc/profile

ingresamos la password de root para poder editar el archivo, y agregamos la sigte linea, ademas debemos agregar la variable al export.

MAVEN_HOME=/home/slack/programas/apache-maven-2.2.1

export ANT_HOME JAVA_HOME CATALINA_HOME TOMCAT_HOME AWT_TOOLKIT=MToolkit MATLAB_JAVA MAVEN_HOME

Tambien debemos agregar el bin de Maven al path, en el archivo enviromment, ejecutamos el sigte. comando, con sudo para poder editar:

slack@zion:~/programas/apache-maven-2.2.1$ sudo nano /etc/environment

y agregamos el bin de Maven al path, como se muestra abajo:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/slack/programas/apache-maven-2.2.1/bin"

Una vez hecho esto debemos cerrar la consola, y esperar que los demonios del sistema actualicen las variables, en caso contrario bastara con reiniciar. Para confirmar que tenemos bien configurado Maven ejecutamos:

slack@zion:~/programas/apache-maven-2.2.1$ mvn --version
Apache Maven 2.2.1 (r801777; 2009-08-06 15:16:01-0400)
Java version: 1.5.0_12
Java home: /home/slack/programas/java/jdk1.5.0_12/jre
Default locale: es_CL, platform encoding: UTF-8
OS name: "linux" version: "2.6.28-15-generic" arch: "i386" Family: "unix"

Esto nos despliega la informacion del contexto para ejecutar Maven (mvn)

Ahora ya podemos ejecutar mvn para nuestros proyectos, debemos para este ejemplo tener instalado Mysql, utilizaremos mvn para appfuse2, entonces vamos al directorio de nuestros proyectos, y ejecutamos la sigte. linea

slack@zion:~/desarrollo$ mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-spring -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=cl.xerox.app -DartifactId=appfuse2

En esta linea le decimos a maven nuestra estructura de packages y el nombre del proyecto. al ejecutar el comando se comienza la descarga de las dependencias del proyecto, mediante POM.xml (proyect object model), este es el log resumido:

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] org.apache.maven.plugins: checking for updates from central
[INFO] org.codehaus.mojo: checking for updates from central
[INFO] artifact org.apache.maven.plugins:maven-archetype-plugin: checking for updates from central
Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/2.0-alpha-4/maven-archetype-plugin-2.0-alpha-4.pom

Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetype/maven-archetype/2.0-alpha-4/maven-archetype-2.0-alpha-4.pom

Downloading: http://repo1.maven.org/maven2/org/apache/maven/maven-parent/9/maven-parent-9.pom

Downloading: http://repo1.maven.org/maven2/org/apache/apache/4/apache-4.pom

Downloading: http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archetype-plugin/2.0-alpha-4/maven-archetype-plugin-2.0-alpha-4.jar

[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:create] (aggregator-style)
[INFO] ------------------------------------------------------------------------
Downloading: http://repo1.maven.org/maven2/org/apache/maven/archetype/archetype-common/2.0-alpha-4/archetype-common-2.0-alpha-4.pom

Downloading: http://repo1.maven.org/maven2/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar

Downloading: http://repo1.maven.org/maven2/org/codehaus/plexus/plexus-velocity/1.1.3/plexus-velocity-1.1.3.jar
Downloading: http://repo1.maven.org/maven2/velocity/velocity-dep/1.4/velocity-dep-1.4.jar

[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:create {execution: default-cli}]
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] Defaulting package to group ID: cl.xerox.app
[INFO] We are using command line specified remote repositories: http://static.appfuse.org/releases
Downloading: http://static.appfuse.org/releases/org/appfuse/archetypes/appfuse-basic-spring/2.0.2/appfuse-basic-spring-2.0.2.jar
77K downloaded (appfuse-basic-spring-2.0.2.jar)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: appfuse-basic-spring:2.0.2
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: cl.xerox.app
[INFO] Parameter: packageName, Value: cl.xerox.app
[INFO] Parameter: basedir, Value: /home/slack/desarrollo
[INFO] Parameter: package, Value: cl.xerox.app
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: artifactId, Value: appfuse2
.
.
.
[INFO] OldArchetype created in dir: /home/slack/desarrollo/appfuse2
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 minutes
[INFO] Finished at: Mon Nov 23 11:56:57 GMT-04:00 2009
[INFO] Final Memory: 9M/67M
[INFO] ------------------------------------------------------------------------
slack@zion:~/desarrollo$


Listo, esto se demora alrededor de unos 5 a 10 min, dependiendo la velocidad de tu red. ahora debemos ejecutar el comando que creara la base de datos (mismo nombre del proyecto) o la seteamos al final del archivo pom.xml, la ejecucion anterior nos creo el proyecto del nuestro directorio de proyectos con el sigte. contenido:

slack@zion:~/desarrollo/appfuse2$ ls -la
total 56
drwxr-xr-x 4 slack slack 4096 2009-11-23 13:40 .
drwxrwxrwx 58 slack slack 4096 2009-11-23 13:55 ..
-rw-r--r-- 1 slack slack 33142 2009-11-23 12:56 pom.xml
-rw-r--r-- 1 slack slack 863 2009-11-23 12:56 README.txt
drwxr-xr-x 5 slack slack 4096 2009-11-23 12:56 src
drwxr-xr-x 9 slack slack 4096 2009-11-23 13:42 target
slack@zion:~/desarrollo/appfuse2$

Entonces ejecutamos, el comando que hara el deploy automatico y descargara las librerias faltantes del proyecto que creamos con el comando anterior, ademas arrancara jetty, Jetty es un servidor HTTP y un contenedor de Servlets escrito en Java y open source.

Para desplegar la aplicación basta con ejecutar mvn jetty:run-war en el directorio del proyecto.

slack@zion:~/desarrollo/appfuse2$ mvn jetty:run-war

este es un resumen de la ejecución del comando, esta tarea se demora aprox 20 min, dependiendo tu velocidad

Downloading: http://static.appfuse.org/repository/jboss/javassist/3.3.ga/javassist-3.3.ga.jar
Downloading: http://static.appfuse.org/repository/freemarker/freemarker/2.3.8/freemarker-2.3.8.jar

Downloading: http://static.appfuse.org/repository/org/apache/maven/reporting/maven-reporting-impl/2.0.4/maven-reporting-impl-2.0.4.jar
225K downloaded (doxia-core-1.0-alpha-7.jar)

Downloading: http://static.appfuse.org/repository/commons-collections/commons-collections/2.1.1/commons-collections-2.1.1.jar
13K downloaded (maven-reporting-impl-2.0.4.jar)
Downloading: http://static.appfuse.org/repository/commons-logging/commons-logging-api/1.0.4/commons-logging-api-1.0.4.jar
[INFO] Unable to find resource 'commons-collections:commons-collections:jar:2.1.1' in repository appfuse (http://static.appfuse.org/repository)

Downloading: http://static.appfuse.org/repository/org/hibernate/hibernate-validator/3.0.0.ga/hibernate-validator-3.0.0.ga.jar
[INFO] Unable to find resource 'org.hibernate:hibernate-validator:jar:3.0.0.ga' in repository appfuse (http://static.appfuse.org/repository)
Downloading: http://static.appfuse.org/repository/org/hibernate/hibernate-validator/3.0.0.ga/hibernate-validator-3.0.0.ga.jar
[INFO] Unable to find resource 'org.hibernate:hibernate-validator:jar:3.0.0.ga' in repository appfuse (http://static.appfuse.org/repository)
Downloading: http://repo1.maven.org/maven2/org/hibernate/hibernate-validator/3.0.0.ga/hibernate-validator-3.0.0.ga.jar

[INFO] [hibernate3:hbm2ddl {execution: default}]
[INFO] Configuration XML file loaded: file:/home/slack/desarrollo/appfuse2/src/main/resources/hibernate.cfg.xml
[INFO] Configuration XML file loaded: file:/home/slack/desarrollo/appfuse2/src/main/resources/hibernate.cfg.xml
[INFO] Configuration Properties file loaded: /home/slack/desarrollo/appfuse2/target/classes/jdbc.properties
alter table user_role drop foreign key FK143BF46AF503D155;
alter table user_role drop foreign key FK143BF46A4FD90D75;
drop table if exists app_user;
drop table if exists role;
drop table if exists user_role;
create table app_user (id bigint not null auto_increment, account_expired bit not null, account_locked bit not null, address varchar(150), city varchar(50) not null, country varchar(100), postal_code varchar(15) not null, province varchar(100), credentials_expired bit not null, email varchar(255) not null unique, account_enabled bit, first_name varchar(50) not null, last_name varchar(50) not null, password varchar(255) not null, password_hint varchar(255), phone_number varchar(255), username varchar(50) not null unique, version integer, website varchar(255), primary key (id)) ENGINE=InnoDB;
create table role (id bigint not null auto_increment, description varchar(64), name varchar(20), primary key (id)) ENGINE=InnoDB;
create table user_role (user_id bigint not null, role_id bigint not null, primary key (user_id, role_id)) ENGINE=InnoDB;
alter table user_role add index FK143BF46AF503D155 (user_id), add constraint FK143BF46AF503D155 foreign key (user_id) references app_user (id);
alter table user_role add index FK143BF46A4FD90D75 (role_id), add constraint FK143BF46A4FD90D75 foreign key (role_id) references role (id);
[WARNING] 2 errors occurred while performing .
[INFO] Unable to find resource 'junit-addons:junit-addons:pom:1.4' in repository appfuse (http://static.appfuse.org/repository)
Downloading: http://repo1.maven.org/maven2/junit-addons/junit-addons/1.4/junit-addons-1.4.pom

Downloading: http://static.appfuse.org/repository/junit/junit/4.0/junit-4.0.jar
Downloading: http://static.appfuse.org/repository/org/apache/maven/surefire/surefire-junit4/2.4.3/surefire-junit4-2.4.3.jar
[INFO] Unable to find resource 'org.apache.maven.surefire:surefire-junit4:jar:2.4.3' in repository appfuse (http://static.appfuse.org/repository)
Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-junit4/2.4.3/surefire-junit4-2.4.3.jar
[INFO] Unable to find resource 'junit:junit:jar:4.0' in repository appfuse (http://static.appfuse.org/repository)
Downloading: http://repo1.maven.org/maven2/junit/junit/4.0/junit-4.0.jar

[INFO] Surefire report directory: /home/slack/desarrollo/appfuse2/target/surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running cl.xerox.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.081 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] [dbunit:operation {execution: test}]
Downloading: http://static.appfuse.org/repository/org/apache/maven/maven-plugin-api/2.0.1/maven-plugin-api-2.0.1.pom
643b downloaded (maven-plugin-api-2.0.1.pom)
Downloading: http://static.appfuse.org/repository/org/apache/maven/maven/2.0.1/maven-2.0.1.pom
11K downloaded (maven-2.0.1.pom)
Downloading: http://static.appfuse.org/repository/org/apache/maven/maven-archiver/2.2/maven-archiver-2.2.pom
1K downloaded (maven-archiver-2.2.pom)
Downloading: http://static.appfuse.org/repository/org/apache/maven/shared/maven-shared-components/3/maven-shared-components-3.pom
1K downloaded (maven-shared-components-3.pom)
Downloading: http://static.appfuse.org/repository/org/codehaus/plexus/plexus-archiver/1.0-alpha-7/plexus-archiver-1.0-alpha-7.pom
1K downloaded (plexus-archiver-1.0-alpha-7.pom)
Downloading: http://static.appfuse.org/repository/org/codehaus/plexus/plexus-utils/1.2/plexus-utils-1.2.pom
767b downloaded (plexus-utils-1.2.pom)
Downloading: http://static.appfuse.org/repository/org/apache/maven/maven-artifact/2.0.1/maven-artifact-2.0.1.pom
765b downloaded (maven-artifact-2.0.1.pom)
Downloading: http://static.appfuse.org/repository/org/apache/maven/maven-archiver/2.2/maven-archiver-2.2.jar
Downloading: http://static.appfuse.org/repository/org/codehaus/plexus/plexus-archiver/1.0-alpha-7/plexus-archiver-1.0-alpha-7.jar
9K downloaded (maven-archiver-2.2.jar)
138K downloaded (plexus-archiver-1.0-alpha-7.jar)
[INFO] [war:war {execution: default-war}]
[INFO] Exploding webapp...
[INFO] Assembling webapp appfuse2 in /home/slack/desarrollo/appfuse2/target/appfuse2-1.0-SNAPSHOT
[INFO] Copy webapp webResources to /home/slack/desarrollo/appfuse2/target/appfuse2-1.0-SNAPSHOT
[INFO] Expanding: /home/slack/.m2/repository/org/appfuse/appfuse-spring/2.0.2/appfuse-spring-2.0.2.war into /home/slack/desarrollo/appfuse2/target/war/work/appfuse-spring-2.0.2
[INFO] Expanding: /home/slack/.m2/repository/org/appfuse/appfuse-web-common/2.0.2/appfuse-web-common-2.0.2.war into /home/slack/desarrollo/appfuse2/target/war/work/appfuse-web-common-2.0.2
[INFO] Overlaying 2 war(s).
[INFO] Generating war /home/slack/desarrollo/appfuse2/target/appfuse2-1.0-SNAPSHOT.war
[INFO] Building war: /home/slack/desarrollo/appfuse2/target/appfuse2-1.0-SNAPSHOT.war
Downloading: http://static.appfuse.org/repository/org/mortbay/jetty/jetty/6.1.9/jetty-6.1.9.pom
[INFO] Unable to find resource 'org.mortbay.jetty:jetty:pom:6.1.9' in repository appfuse (http://static.appfuse.org/repository)
Downloading: http://snapshots.repository.codehaus.org/org/mortbay/jetty/jetty/6.1.9/jetty-6.1.9.pom
[INFO] Unable to find resource 'org.mortbay.jetty:jetty:pom:6.1.9' in repository codehaus.org (http://snapshots.repository.codehaus.org)
Downloading: http://repo1.maven.org/maven2/org/mortbay/jetty/jetty/6.1.9/jetty-6.1.9.pom

Downloading: http://static.appfuse.org/repository/org/mortbay/jetty/jetty-util/6.1.9/jetty-util-6.1.9.pom
[INFO] Unable to find resource 'org.mortbay.jetty:jetty-util:pom:6.1.9' in repository appfuse (http://static.appfuse.org/repository)
Downloading: http://snapshots.repository.codehaus.org/org/mortbay/jetty/jetty-util/6.1.9/jetty-util-6.1.9.pom
[INFO] Unable to find resource 'org.mortbay.jetty:jetty-util:pom:6.1.9' in repository codehaus.org (http://snapshots.repository.codehaus.org)
Downloading: http://static.appfuse.org/repository/org/mortbay/jetty/jetty-util/6.1.9/jetty-util-6.1.9.pom
[INFO] Unable to find resource 'org.mortbay.jetty:jetty-util:pom:6.1.9' in repository appfuse (http://static.appfuse.org/repository)
Downloading: http://repo1.maven.org/maven2/org/mortbay/jetty/jetty-util/6.1.9/jetty-util-6.1.9.pom

Downloading: http://static.appfuse.org/repository/org/mortbay/jetty/servlet-api-2.5/6.1.9/servlet-api-2.5-6.1.9.pom
[INFO] Unable to find resource 'org.mortbay.jetty:servlet-api-2.5:pom:6.1.9' in repository appfuse (http://static.appfuse.org/repository)
Downloading: http://snapshots.repository.codehaus.org/org/mortbay/jetty/servlet-api-2.5/6.1.9/servlet-api-2.5-6.1.9.pom
[INFO] Unable to find resource 'org.mortbay.jetty:servlet-api-2.5:pom:6.1.9' in repository codehaus.org (http://snapshots.repository.codehaus.org)
Downloading: http://static.appfuse.org/repository/org/mortbay/jetty/servlet-api-2.5/6.1.9/servlet-api-2.5-6.1.9.pom
[INFO] Unable to find resource 'org.mortbay.jetty:servlet-api-2.5:pom:6.1.9' in repository appfuse (http://static.appfuse.org/repository)
Downloading: http://repo1.maven.org/maven2/org/mortbay/jetty/servlet-api-2.5/6.1.9/servlet-api-2.5-6.1.9.pom

[INFO] [jetty:run-war {execution: default-cli}]
[INFO] Configuring Jetty for project: AppFuse Spring MVC Application
2009-11-23 12:42:31.506::INFO: Logging to STDERR via org.mortbay.log.StdErrLog
[INFO] Context path = /
[INFO] Tmp directory = determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] Starting jetty 6.1.9 ...
2009-11-23 12:42:31.648::INFO: jetty-6.1.9
2009-11-23 12:42:31.667::INFO: Extract jar:file:/home/slack/desarrollo/appfuse2/target/appfuse2-1.0-SNAPSHOT.war!/ to /home/slack/desarrollo/appfuse2/target/work/webapp
2009-11-23 12:42:32.062::INFO: No Transaction manager found - if your webapp requires one, please configure one.
2009-11-23 12:42:34.317:/:INFO: Initializing Spring root WebApplicationContext
2009-11-23 12:42:44.535:/:INFO: Initializing Spring FrameworkServlet 'dispatcher'
2009-11-23 12:42:45.031::INFO: Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 3 seconds.
^C2009-11-23 13:38:54.748::INFO: Shutdown hook executing
2009-11-23 13:38:55.286:/:INFO: Destroying Spring FrameworkServlet 'dispatcher'
2009-11-23 13:38:55.289:/:INFO: Closing Spring root WebApplicationContext
2009-11-23 13:38:55.297::INFO: Shutdown hook complete
[INFO] Jetty server exiting.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 76 minutes 29 seconds
[INFO] Finished at: Mon Nov 23 13:38:55 GMT-04:00 2009
[INFO] Final Memory: 37M/84M
[INFO] ------------------------------------------------------------------------
slack@zion:~/desarrollo/appfuse2$

Vemos ahí que tambien esta la configuiración de la base de datos, creacion de tablas, test de aplicacion y levantamiento del servidor jetty en el puerto 8080. ahora solo vamos al browser y abrimos la url: http://localhost:8080/login.jsp

user: admin
passwd: admin




Saludos y suerte !!
bee free.... use linux..

No hay comentarios: