Skip to content
lifuzu edited this page Nov 22, 2013 · 2 revisions

Create initial Maven project

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

Compile the Maven project

cd my-app
mvn compile

Test the Maven project

cd my-app
mvn test

Create a jar package

mvn package

Install the package in local repository

mvn install

Clean the Maven project

mvn clean

Create an IntelliJ IDEA descriptor for the project

mvn idea:idea

For Eclipse:

mvn eclipse:eclipse

Add a dependency for the Maven project

<project>
...
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
...
</project>
# The scope element indicates how your project uses that dependency, and can be values like compile, test, and runtime.

Build more than one project at once

  1. Create another project:
mvn archetype:generate \
    -DarchetypeGroupId=org.apache.maven.archetypes \
    -DarchetypeArtifactId=maven-archetype-webapp \
    -DgroupId=com.mycompany.app \
    -DartifactId=my-webapp
  1. Add a parent pom.xml file in the directory above the other two:
+- pom.xml
+- my-app
| +- pom.xml
| +- src
|   +- main
|     +- java
+- my-webapp
| +- pom.xml
| +- src
|   +- main
|     +- webapp
  1. The pom.xml should contain:
<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.mycompany.app</groupId>
  <artifactId>app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>my-app</module>
    <module>my-webapp</module>
  </modules>
</project>
  1. Need a dependency on the JAR from webapp, add this to my-webapp/pom.xml:
  ...
  <dependencies>
    <dependency>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    ...
  </dependencies>
  1. Add the following elemnt to both of the other pom.xml files in the subdirections:
<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">
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>app</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  ...
  1. Now, try it... from the top level directory, run:
mvn clean install
  1. Verify the artifacts:
jar tvf my-webapp/target/my-webapp.war

SAMPLE REPO:

https://github.com/lifuzu/study-maven

#REFERENCE:

  1. http://maven.apache.org/guides/getting-started/index.html

Clone this wiki locally