I will not explain the use of maven, repositories and dependencies because that is very well documented and you can search it in google :P
Shall we start?
First a shortly explanation of the maven simple archetype "maven-archetype-quickstart":
When you use the following command line
mvn archetype:generate -DgroupId:com.mycompany.app -DartifactId=my-app -Dversion=1.0.0 -DarchetypeArtificatId=maven-archetype-quickstart or you click in
eclipse -> file -> new -> project -> maven .. and next next next..
Maven will use the archetype to create a standard folders with your package name convention and a main class with their unit testing.
That is an archetype, only a template, that follows your rules (the parameters with the -D java option) to create your project.
Building our first archetype:
Create a eclipse project with this structure:
Its important to put the source folders of our template into the archetype-resources folder. All content here must be copied to our source folder with our package conventions when we used the mvn command line to build this artifact.
The META-INF/maven/archetype.xml is the main file, because this must explicity indicate all the resources we want to be copied, all the clases, spring files, etc.
An example:
<?xml version="1.0" encoding="UTF-8"?>
<archetype
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 http://maven.apache.org/xsd/archetype-1.0.0.xsd">
<id>fusion</id>
<sources>
</sources>
<testSources>
</testSources>
<resources>
</resources>
</archetype>
Here we put the sources, the testsources, and the resources of our template.
Now, we must add the dependencies for our project, open the pom.xml in the archetype-resources folder, not the pom.xml in the project folder, because the pom we want to modify isnt the pom of this project.
And copy this
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- FUSION ARCHETYPE -->
<!--
Builded by Tomas de Priede. The goal is build simply and fastly a
spring oriented application with the following features: * An Spring
mvc with freemarker templates. * An enviroment configured to do unit
and integration tests. * JMX Capabilities to monitor the transactions.
* JMS Capabilities to send message to other java applications. * Most
common entities mapped using hibernate. * Hibernate ehcache
configured. * A JBPM Workflow configured * A mail system using the
jbpm
-->
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>war</packaging>
<name>A custom project</name>
<url>http://www.myorganization.org</url>
<properties>
<!-- THIRD PARTY LIBRARIES PROPERTIES -->
<org.springframework.version>3.0.3.RELEASE</org.springframework.version>
<org.springframework.webflow.version>2.0.8.RELEASE</org.springframework.webflow.version>
<aspectj.version>1.6.8</aspectj.version>
<cglib.version>2.2</cglib.version>
<tuckey.version>3.1.0</tuckey.version>
<apache.tiles.version>2.2.1</apache.tiles.version>
<apachemq.version>5.1.0</apachemq.version>
<servlet-api.version>2.5</servlet-api.version>
<jsp-api.version>2.1</jsp-api.version>
<jstl.version>1.2</jstl.version>
<el-api.version>1.0</el-api.version>
<joda-time.version>1.6</joda-time.version>
<javassist.version>3.11.0.GA</javassist.version>
<hibernate.annotations.version>3.4.0.GA</hibernate.annotations.version>
<hibernate.version>3.3.2.GA</hibernate.version>
<ehcache.version>2.0.0</ehcache.version>
<commons-dbcp.version>1.2.1</commons-dbcp.version>
<commons-io.version>1.4</commons-io.version>
<commons-digester.version>2.0</commons-digester.version>
<commons-fileupload.version>1.2.1</commons-fileupload.version>
<commons-lang.version>2.4</commons-lang.version>
<org.slf4j.version>1.4.2</org.slf4j.version>
<hsqldb.version>1.8.0.10</hsqldb.version>
<junit.version>4.7</junit.version>
<easymock.version>3.0</easymock.version>
<!-- END OF THIRD PARTY LIBRARIES PROPERTIES -->
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<!-- Spring WebFlow -->
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-js</artifactId>
<version>${org.springframework.webflow.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- ACTIVE MQ -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>${apachemq.version}</version>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>${commons-digester.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>${hibernate.annotations.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</exclusion>
<exclusion>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache.version}</version>
<type>pom</type>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- URL Rewrite -->
<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>${tuckey.version}</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Apache Tiles -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>${apache.tiles.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>${apache.tiles.version}</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>${el-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>${easymock.version}</version>
<scope>test</scope>
</dependency>
<!-- HSQLDB -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>sources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<resources>
<resource>
<directory>${basedir}/src/test</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
If you put attention the groupId artifactId and version, must be replaced with the real ones when you build the artifact, this happen because maven uses Apache Velocity to replace these expresions.
The pom.xml in the archetype-resources and the sources, testsources and resources folder
not need to be in in the archetype.xml so we dont added.
Now install this artifact:
Run the mvn install in the folder of the fusion project or run directly from eclipse.
mvn archetype:create -DarchetypeGroupId=ar.com.fusion -DarchetypeArtifactId=fusion -DarchetypeVersion=1.0.0 -DgroupId=ar.com.prueba -DartifactId=prueba -Dversion=1.0
replace the last three parameter if you want to.
Import the created project to eclipse and you will see the maven pom.xml with the arguments replaced and all the dependencies inserted.
The next post will be viewing how to add some classes implementing a hibernate association many to many, and their test.
See you in the next post!
Tom

No comments:
Post a Comment