Skip to content

Instantly share code, notes, and snippets.

@theotherian
Created July 9, 2013 04:59
Show Gist options
  • Save theotherian/5954823 to your computer and use it in GitHub Desktop.
Save theotherian/5954823 to your computer and use it in GitHub Desktop.
Maven Archetype how tos Part 3
Files for creating a Maven Archtype - part 3
<archetype-descriptor name="sample-archetype">
<fileSets>
<fileSet filtered="true">
<directory></directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-model</directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-model/src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-model/src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-webapp</directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-webapp/src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-webapp/src/main/webapp/WEB-INF</directory>
<includes>
<include>web.xml</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
<archetype-descriptor name="sample-archetype">
<fileSets>
<fileSet filtered="true">
<directory></directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-model</directory>
<includes>
<include>pom.xml</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-model/src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory>__artifactId__-model/src/test/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
$ cd sample-project
$ mvn clean install
mkdir -p src/main/resources/archetype-resources/__artifactId__-webapp/src/main/java/__packageInPathFormat__
mkdir -p src/main/resources/archetype-resources/__artifactId__-webapp/src/main/webapp/WEB-INF
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}-model</artifactId>
<version>${version}</version>
</dependency>
</dependencies>
</dependencyManagement>
$ tree
.
|____pom.xml
|____src
| |____main
| | |____resources
| | | |____archetype-resources
| | | | |______artifactId__-model
| | | | | |____pom.xml
| | | | | |____src
| | | | | | |____main
| | | | | | | |____java
| | | | | | | | |______packageInPathFormat__
| | | | | | | | | |____App.java
| | | | | | |____test
| | | | | | | |____java
| | | | | | | | |______packageInPathFormat__
| | | | | | | | | |____AppTest.java
| | | | |______artifactId__-webapp
| | | | | |____pom.xml
| | | | | |____src
| | | | | | |____main
| | | | | | | |____java
| | | | | | | | |______packageInPathFormat__
| | | | | | | | | |____Servlet.java
| | | | | | | |____webapp
| | | | | | | | |____WEB-INF
| | | | | | | | | |____web.xml
| | | | |____pom.xml
| | | |____META-INF
| | | | |____maven
| | | | | |____archetype-metadata.xml
$ tree
.
|____pom.xml
|____src
| |____main
| | |____resources
| | | |____archetype-resources
| | | | |____pom.xml
| | | | |____src
| | | | | |____main
| | | | | | |____java
| | | | | | | |______packageInPathFormat__
| | | | | | | | |____App.java
| | | | | |____test
| | | | | | |____java
| | | | | | | |______packageInPathFormat__
| | | | | | | | |____AppTest.java
| | | |____META-INF
| | | | |____maven
| | | | | |____archetype-metadata.xml
$ mvn clean install
$ cd ..
$ mvn archetype:generate -DarchetypeCatalog=local
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: local -> com.theotherian.archetype:sample-archetype (sample-archetype)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1: 1
Define value for property 'groupId': : com.theotherian
Define value for property 'artifactId': : sample-project
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': com.theotherian: :
Confirm properties configuration:
groupId: com.theotherian
artifactId: sample-project
version: 1.0-SNAPSHOT
package: com.theotherian
Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: sample-archetype:1.0-SNAPSHOT
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.theotherian
[INFO] Parameter: artifactId, Value: sample-project
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.theotherian
[INFO] Parameter: packageInPathFormat, Value: com/theotherian
[INFO] Parameter: package, Value: com.theotherian
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.theotherian
[INFO] Parameter: artifactId, Value: sample-project
[WARNING] Don't override file /Users/isimpson/sample-projects/sample-project/pom.xml
[INFO] project created from Archetype in dir: /Users/isimpson/sample-projects/sample-project
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.574s
[INFO] Finished at: Sat May 12 22:14:07 MDT 2012
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------
<modules>
<module>${artifactId}-model</module>
<module>${artifactId}-webapp</module>
</modules>
$ mkdir src/main/resources/archetype-resources/__artifactId__-model
$ mv src/main/resources/archetype-resources/src src/main/resources/archetype-resources/__artifactId__-model/
$ mv src/main/resources/archetype-resources/pom.xml src/main/resources/archetype-resources/__artifactId__-model/
<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>
<parent>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</parent>
<artifactId>${artifactId}-model</artifactId>
<packaging>jar</packaging>
<name>${artifactId}-model</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
</dependency>
</dependencies>
</project>
<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>
<parent>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</parent>
<artifactId>${artifactId}-model</artifactId>
<packaging>jar</packaging>
<name>${artifactId}-model</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<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>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>pom</packaging>
<name>${artifactId}</name>
<modules>
<module>${artifactId}-model</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<inherited>true</inherited>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
<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>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>pom</packaging>
<name>${artifactId}</name>
<modules>
<module>${artifactId}-model</module>
</modules>
</project>
<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>
<parent>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</parent>
<artifactId>${artifactId}-webapp</artifactId>
<packaging>war</packaging>
<name>${artifactId}-webapp</name>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}-model</artifactId>
</dependency>
</dependencies>
</project>
$ cd sample-project-webapp
$ mvn jetty:run-war
package ${package};
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setStatus(200);
PrintWriter writer = response.getWriter();
writer.write(App.message());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<display-name>${artifactId}</display-name>
<servlet-name>${artifactId}</servlet-name>
<servlet-class>${package}.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>${artifactId}</servlet-name>
<url-pattern>/${artifactId}</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment