Back to Maven

See Also Build Automation

Maven Getting Started

with a Multiple Module Project

1. How do I Create a Multiple Module Project?

1.1. Create POM Root Project

## -B Run in non-interactive (batch) mode
mvn -B archetype:generate \
  -DarchetypeGroupId=org.codehaus.mojo.archetypes \
  -DarchetypeArtifactId=pom-root \
  -DarchetypeVersion=RELEASE \
  -DgroupId=com.li3huo.server \
  -DartifactId=server-project \
  -Dversion=1.0-SNAPSHOT

注意以上命令第4行的-DarchetypeArtifactId=pom-root,说明要生成的是一个根项目

如果执行成功了,会建立server-project目录,目录下有一个pom.xml, ➜  w2015  cat server-project/pom.xml

<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>com.li3huo.server</groupId>
  <artifactId>server-project</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>server-project</name>
</project>

1.1.1. Basic Knowledges

1.2. Create Modules

cd server-project
##Create Modules
mvn -B archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DarchetypeVersion=RELEASE \
  -DgroupId=com.li3huo.server \
  -DartifactId=netty-module \
  -Dversion=1.0-SNAPSHOT \
  -Dpackage=com.li3huo.netty5
mvn archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DarchetypeVersion=RELEASE \
  -DgroupId=com.li3huo.server \
  -DartifactId=spring3-module \
  -Dversion=1.0-SNAPSHOT \
  -Dpackage=com.li3huo.spring3 \
  -DinteractiveMode=false
mvn eclipse:eclipse

➜  server-project  tree -d
.
├── netty-module
│   └── src
│       ├── main
│       │   └── java
│       │       └── com
│       │           └── li3huo
│       │               └── netty5
│       └── test
...
└── spring3-module
...

24 directories

1.2.1. Basic Knowledges

2. How do I compile my application sources?

➜  server-project  mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] server-project
[INFO] netty-module
[INFO] spring3-module
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] server-project ..................................... SUCCESS [  0.001 s]
[INFO] netty-module ....................................... SUCCESS [  0.666 s]
[INFO] spring3-module ..................................... SUCCESS [  0.023 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.773 s
[INFO] Finished at: 2015-01-08T17:57:00+08:00
[INFO] Final Memory: 13M/245M
[INFO] ------------------------------------------------------------------------

3. How do I compile my test sources and run my unit tests?

➜  server-project  mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] server-project
[INFO] netty-module
[INFO] spring3-module
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building server-project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building netty-module 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.li3huo.netty5.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec

Results :

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

[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building spring3-module 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.li3huo.spring3.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] server-project ..................................... SUCCESS [  0.002 s]
[INFO] netty-module ....................................... SUCCESS [  1.030 s]
[INFO] spring3-module ..................................... SUCCESS [  0.182 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.298 s
[INFO] Finished at: 2015-01-08T18:02:07+08:00
[INFO] Final Memory: 15M/309M
[INFO] ------------------------------------------------------------------------

4. How do I create a JAR and install it in my local repository?

➜  server-project  mvn package
➜  server-project  mvn install

5. How do I use plug-ins?

Configure the Java compiler to allow JDK 1.8 sources

...
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
  </plugins>
</build>
...

5.1. Basic Knowledges

6. How do I add resources to my JAR?

Refer to http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR

my-app
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |   `-- com
    |   |       `-- mycompany
    |   |           `-- app
    |   |               `-- App.java
    |   `-- resources
    |       `-- META-INF
    |           `-- application.properties
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

Unpacked the JAR, it you would like the following:

|-- META-INF
|   |-- MANIFEST.MF
|   |-- application.properties
|   `-- maven
|       `-- com.mycompany.app
|           `-- my-app
|               |-- pom.properties
|               `-- pom.xml
`-- com
    `-- mycompany
        `-- app
            `-- App.class

7. How do I filter resource files?

Refer to http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files

➜  server-project  mvn tbc

8. How do I use external dependencies?

➜  server-project  mvn tbc

8.1. Basic Knowledges

9. How do I copy my files on my machine?

                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-dependency-plugin</artifactId>
                                <version>2.8</version>
                                <configuration>
                                        <artifactItems>
                                                <artifactItem>
                                                        <groupId>li3huo.jmeter</groupId>
                                                        <artifactId>jmeter-add-ons</artifactId>
                                                        <version>1.0-SNAPSHOT</version>
                                                        <overWrite>true</overWrite>
                                                        <outputDirectory>${jmeter.lib.ext}</outputDirectory>
                                                </artifactItem>
                                                <artifactItem>
                                                        <groupId>com.alibaba</groupId>
                                                        <artifactId>fastjson</artifactId>
                                                        <version>1.1.33</version>
                                                        <overWrite>true</overWrite>
                                                        <outputDirectory>${jmeter.lib.ext}</outputDirectory>
                                                </artifactItem>
                                        </artifactItems>
                                        <!-- other configurations here -->
                                </configuration>
                        </plugin>   

mvn install dependency:copy

9.1. Basic Knowledges

10. How do I deploy my jar in my remote repository?

➜  server-project  mvn tbc

10.1. Basic Knowledges

11. How do I build other types of projects?

mvn -B archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DarchetypeArtifactId=maven-archetype-site \
  -DgroupId=com.mycompany.app \
  -DartifactId=my-app-site

mvn -B archetype:generate \
    -DarchetypeGroupId=org.apache.maven.archetypes \
    -DarchetypeArtifactId=maven-archetype-webapp \
    -DgroupId=com.mycompany.app \
    -DartifactId=my-webapp

11.1. Basic Knowledges

archetypes

12. How to Download source and Javadoc manually?

https://stackoverflow.com/questions/5780758/maven-always-download-sources-and-javadocs

mvn dependency:sources
mvn dependency:resolve -Dclassifier=javadoc
mvn clean eclipse:eclipse -DdownloadSources=true

<settings>

   <!-- ... other settings here ... -->

    <profiles>
        <profile>
            <id>downloadSources</id>
            <properties>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </properties>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>downloadSources</activeProfile>
    </activeProfiles>
</settings>

12.1. Basic Knowledges

archetypes

13. Reference

MainWiki: mvn_getting_started (last edited 2015-01-07 23:31:17 by twotwo)