Wednesday, February 25, 2009

Quick Maven Commands

Creating a Maven project
In order to create a new maven project called MyProject run the following command:
mvn archetype:create -DgroupId=fs.work -DartifactId=MyProject
This will create a new directory called MyProject with a pom.xml and the following tree structure:
MyProject
 |-->pom.xml
 |-->src
 |  |-->main
 |  |  |-->java
 |  |  |  |-->fs
 |  |  |  |  |-->work
 |  |  |  |  |  |-->App.java
 |  |-->test
 |  |  |-->java
 |  |  |  |-->fs
 |  |  |  |  |-->work
 |  |  |  |  |  |-->AppTest.java
The pom file looks like this:

  4.0.0
  fs.work
  MyProject
  jar
  1.0-SNAPSHOT
  MyProject
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
  

Creating sub-modules
If you need to create sub-modules within your project, you need to change the packaging in the pom file (i.e. the "super" pom), to pom. Then, from within the MyProject directory issue the following commands to create sub-modules:
mvn archetype:create -DgroupId=fs.work -DartifactId=MyProjectWeb -Dpackaging=war
mvn archetype:create -DgroupId=fs.work -DartifactId=MyProjectModule1 -Dpackaging=jar
This creates the sub-modules and the directory tree now looks like this:
MyProject
 |-->pom.xml
 |-->src
 |  |-->main
 |  |  |-->java
 |  |  |  |-->fs
 |  |  |  |  |-->work
 |  |  |  |  |  |-->App.java
 |  |-->test
 |  |  |-->java
 |  |  |  |-->fs
 |  |  |  |  |-->work
 |  |  |  |  |  |-->AppTest.java
 |-->MyProjectModule1
 |  |-->pom.xml
 |  |-->src
 |  |  |-->main
 |  |  |  |-->java
 |  |  |  |  |-->fs
 |  |  |  |  |  |-->work
 |  |  |  |  |  |  |-->App.java
 |  |  |-->test
 |  |  |  |-->java
 |  |  |  |  |-->fs
 |  |  |  |  |  |-->work
 |  |  |  |  |  |  |-->AppTest.java
 |-->MyProjectWeb
 |  |-->pom.xml
 |  |-->src
 |  |  |-->main
 |  |  |  |-->java
 |  |  |  |  |-->fs
 |  |  |  |  |  |-->work
 |  |  |  |  |  |  |-->App.java
 |  |  |-->test
 |  |  |  |-->java
 |  |  |  |  |-->fs
 |  |  |  |  |  |-->work
 |  |  |  |  |  |  |-->AppTest.java
The pom file for MyProjectModule1 contains a reference to the parent and looks like this:

  
    MyProject
    fs.work
    1.0-SNAPSHOT
  
  4.0.0
  fs.work
  MyProjectModule1
  MyProjectModule1
  1.0-SNAPSHOT
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
  

Deploying a jar to the repository
If you have a jar file called myarchive.jar which you want to upload to your maven repository, use the following command:
mvn deploy:deploy-file -Durl=scp://hostname/dir/to/maven -DrepositoryId=fs.repo -Dfile=myarchive.jar -DgroupId=fs.work -DartifactId=myarchive -Dversion=1.0 -Dpackaging=jar
This will create dir/to/maven/fs/work/myarchive/1.0/myarchive-1.0.jar in the maven repository.

Creating a dependency
To create a dependency on myarchive.jar, add the following dependency to your pom:


  fs.work
  myarchive
  1.0

Generating Eclipse .project and .classpath files
Use the following command:
mvn eclipse:eclipse
Skipping tests
To skip tests use the property maven.test.skip=true.
mvn -Dmaven.test.skip=true install
Release a project
Two commands must be invoked, in the following order:
mvn release:prepare
mvn release:perform
Other commands
mvn install
mvn clean
mvn compile
mvn jar:jar

2 comments:

  1. Your mvn commands seems a bit long (without scrolling). I think you may improve it by using a JS code formatter such as Prettify or SyntaxHightlighter

    :-)

    ReplyDelete
  2. I have compiled a mini guide command reference for maven. You can see it here http://www.shankh.com/2009/07/12/maven-commands-reference-mini-guide/

    ReplyDelete