---
title: "Maven Basic"
author: "asran"
date: "2010-12-22T13:20:49Z"
tags: []
categories: ["编程语言"]
slug: "maven-basic"
draft: false
lastmod: "2010-12-22T13:20:49Z"
id: 849247
url: "https://asran.iteye.com/blog/849247"
---

0\. useful commands:

    mvn install:install-file -DgroupId=org.apache.maven.archetypes -DartifactId=${myArtifactId} -Dversion=RELEASE -Dpackaging=jar -Dfile=/path/to/file

    mvn deploy:deploy-file -DgroupId=org.apache.maven.archetypes -DartifactId=${myArtifactId} -Dversion=RELEASE -Dpackaging=jar -Dfile=/path/to/file -Durl=\[url] -DrepositoryId=\[id]

    mvn -Dtest=MenuItemFacadeTest test    

    mvn archetype:create -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=${groupId} -DartifactId=${artifactId}

    mvn archetype:create -DgroupId=${groupId} -DartifactId=${artifactId}

    mvn archetype:create                                      \\

        -DarchetypeGroupId=&lt;archetype-groupId&gt;                \\

        -DarchetypeArtifactId=&lt;archetype-artifactId&gt;          \\

        -DarchetypeVersion=&lt;archetype-version&gt;                \\

        -DgroupId=&lt;my.groupid&gt;                                \\

        -DartifactId=&lt;my-artifactId&gt;

 

1\. setting resource files:

    &lt;build&gt;

        &lt;resources&gt;

            &lt;resource&gt;

                &lt;directory&gt;src/main/java&lt;/directory&gt;

                &lt;includes&gt;

                    &lt;include&gt;\*\*/\*.xml&lt;/include&gt;

                    &lt;include&gt;\*\*/\*.tld&lt;/include&gt;

                &lt;/includes&gt;

            &lt;/resource&gt;

            &lt;resource&gt;

                &lt;directory&gt;src/main/resources&lt;/directory&gt;

                &lt;includes&gt;

                    &lt;include&gt;\*\*/\*.xml&lt;/include&gt;

                    &lt;include&gt;\*\*/\*.properties&lt;/include&gt;

                &lt;/includes&gt;

            &lt;/resource&gt;

        &lt;/resources&gt;

    &lt;/build&gt;

 

2\. compiler plugin:

    &lt;plugin&gt;

        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;

        &lt;version&gt;2.3&lt;/version&gt;

        &lt;configuration&gt;

            &lt;source&gt;1.6&lt;/source&gt;

            &lt;target&gt;1.6&lt;/target&gt;

            &lt;encoding&gt;UTF-8&lt;/encoding&gt;

        &lt;/configuration&gt;

    &lt;/plugin&gt;

 

3\. tomcat plugin(mvn tomcat:run):

    &lt;plugin&gt;

        &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;

        &lt;artifactId&gt;tomcat-maven-plugin&lt;/artifactId&gt;

        &lt;version&gt;1.0-beta-1&lt;/version&gt;

        &lt;configuration&gt;

            &lt;path&gt;/trsWeb&lt;/path&gt;

            &lt;port&gt;9080&lt;/port&gt;

        &lt;/configuration&gt;

    &lt;/plugin&gt;

 

4\. jetty plugin(mvn jetty:run):

    &lt;plugin&gt;

        &lt;groupId&gt;org.mortbay.jetty&lt;/groupId&gt;

        &lt;artifactId&gt;maven-jetty-plugin&lt;/artifactId&gt;

        &lt;version&gt;6.1.24&lt;/version&gt;

        &lt;configuration&gt;

            &lt;scanIntervalSeconds&gt;10&lt;/scanIntervalSeconds&gt;

            &lt;contextPath&gt;/trsWeb&lt;/contextPath&gt;

            &lt;connectors&gt;

                &lt;connector implementation="org.mortbay.jetty.bio.SocketConnector"&gt;

                    &lt;port&gt;9080&lt;/port&gt;

                    &lt;maxIdleTime&gt;60000&lt;/maxIdleTime&gt;

                &lt;/connector&gt;

            &lt;/connectors&gt;

        &lt;/configuration&gt;

    &lt;/plugin&gt;

 

    jetty-debug.bat:

    @echo off

    if "%OS%" == "Windows\_NT" setlocal

    set MAVEN\_OPTS=-Xdebug -Xmx512m -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt\_socket,address=4000,server=y,suspend=n

    mvn jetty:run

 

5\. antrun plugin(mvn package):

    &lt;plugin&gt;

        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;

        &lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;

        &lt;version&gt;1.4&lt;/version&gt;

        &lt;executions&gt;

            &lt;execution&gt;

                &lt;id&gt;package&lt;/id&gt;

                &lt;phase&gt;package&lt;/phase&gt;

                &lt;configuration&gt;

                    &lt;tasks if="${boolVariable}"&gt;

                        &lt;property name="compile\_classpath" refid="maven.compile.classpath"/&gt;

                        &lt;property name="runtime\_classpath" refid="maven.runtime.classpath"/&gt;

                        &lt;property name="test\_classpath" refid="maven.test.classpath"/&gt;

                        &lt;property name="plugin\_classpath" refid="maven.plugin.classpath"/&gt;

 

                        &lt;echo message="compile classpath: ${compile\_classpath}"/&gt;

                        &lt;echo message="runtime classpath: ${runtime\_classpath}"/&gt;

                        &lt;echo message="test classpath:    ${test\_classpath}"/&gt;

                        &lt;echo message="plugin classpath:  ${plugin\_classpath}"/&gt;

 

                        &lt;copy file="d:/1.txt" tofile="d:/2.txt"/&gt;

                    &lt;/tasks&gt;

                &lt;/configuration&gt;

                &lt;goals&gt;

                    &lt;goal&gt;run&lt;/goal&gt;

                &lt;/goals&gt;

            &lt;/execution&gt;

        &lt;/executions&gt;

    &lt;/plugin&gt;

 

    or: mvn antrun:run

 

    &lt;plugin&gt;

        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;

        &lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;

        &lt;version&gt;1.4&lt;/version&gt;

        &lt;configuration&gt;

            &lt;tasks if="${build.prod}"&gt;

                &lt;property name="basedir" value="${basedir}" /&gt;

                &lt;property name="compile\_classpath" refid="maven.compile.classpath" /&gt;

                &lt;property name="runtime\_classpath" refid="maven.runtime.classpath" /&gt;

                &lt;property name="test\_classpath" refid="maven.test.classpath" /&gt;

                &lt;property name="plugin\_classpath" refid="maven.plugin.classpath" /&gt;

                &lt;property name="build.dir" value="${project.build.directory}" /&gt;

                &lt;property name="dist" value="${build.dir}/dist" /&gt;

                &lt;property name="dist.src" value="${dist}/src" /&gt;

                &lt;ant antfile="${basedir}/m2-build.xml"&gt;

                    &lt;target name="${build.task}" /&gt;

                &lt;/ant&gt;

            &lt;/tasks&gt;

        &lt;/configuration&gt;

    &lt;/plugin&gt;

 

6\. profiles.xml

    &lt;?xml version="1.0" encoding="UTF-8"?&gt;

    &lt;profilesXml&gt;

        &lt;profiles&gt;

            &lt;profile&gt;

                &lt;id&gt;development&lt;/id&gt;

                &lt;properties&gt;

                    &lt;maven.test.skip&gt;true&lt;/maven.test.skip&gt;

                &lt;/properties&gt;

            &lt;/profile&gt;

            &lt;profile&gt;

                &lt;id&gt;production&lt;/id&gt;

                &lt;properties&gt;

                    &lt;maven.test.skip&gt;false&lt;/maven.test.skip&gt;

                &lt;/properties&gt;

            &lt;/profile&gt;

        &lt;/profiles&gt;

        &lt;activeProfiles&gt;

            &lt;activeProfile&gt;development&lt;/activeProfile&gt;

        &lt;/activeProfiles&gt;

    &lt;/profilesXml&gt;

    tip: the maven can auto detect the profiles.xml at the same directory with pom.xml, and use it.

 

7\. active profile dynamically:

    1. write activation condition in each profile:

        &lt;activation&gt;

            &lt;property&gt;

                &lt;name&gt;env&lt;/name&gt;

                &lt;value&gt;was&lt;/value&gt;

            &lt;/property&gt;

        &lt;/activation&gt;

    2. define the default ${env} property in pom.xml:

        &lt;properties&gt;

            &lt;env&gt;dev&lt;/env&gt;

        &lt;/properties&gt;

    3. don't specify any activiate profile in neither pom.xml nor profiles.xml

    4. mvn package -Denv=was

 

8\. encoding resource copying:

    &lt;plugin&gt;

        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;

        &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;

        &lt;configuration&gt;

            &lt;encoding&gt;UTF-8&lt;/encoding&gt;

        &lt;/configuration&gt;

    &lt;/plugin&gt;

 

9\. 3 ways to run Java main from Maven£º

    1) Running from Command line

        01. mvn compile

        02. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" &lt;------- no args

        03. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2" -Dexec.commandlineArgs="-Xloggc:&lt;file&gt;"  &lt;------- with args

        04. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime &lt;------- with runtime dependencies in classpath

    2) Running in a phase in pom.xml

        01. &lt;plugin&gt;

                &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;

                &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;

                &lt;version&gt;1.2&lt;/version&gt;

                &lt;executions&gt;

                    &lt;execution&gt;

                        &lt;phase&gt;test&lt;/phase&gt;

                        &lt;goals&gt;

                            &lt;goal&gt;java&lt;/goal&gt;

                        &lt;/goals&gt;

                        &lt;configuration&gt;

                            &lt;mainClass&gt;com.vineetmanohar.module.CodeGenerator&lt;/mainClass&gt;

                            &lt;arguments&gt;

                                &lt;argument&gt;arg0&lt;/argument&gt;

                                &lt;argument&gt;arg1&lt;/argument&gt;

                            &lt;/arguments&gt;

                        &lt;/configuration&gt;

                    &lt;/execution&gt;

                &lt;/executions&gt;

            &lt;/plugin&gt;

        02. mvn test

    3) Running in a profile in pom.xml

        01. &lt;profiles&gt;

                &lt;profile&gt;

                    &lt;id&gt;code-generator&lt;/id&gt;

                    &lt;build&gt;

                        &lt;plugins&gt;

                            &lt;plugin&gt;

                                &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;

                                &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;

                                &lt;version&gt;1.2&lt;/version&gt;

                                &lt;executions&gt;

                                    &lt;execution&gt;

                                        &lt;phase&gt;test&lt;/phase&gt;

                                        &lt;goals&gt;

                                            &lt;goal&gt;java&lt;/goal&gt;

                                        &lt;/goals&gt;

                                        &lt;configuration&gt;

                                            &lt;mainClass&gt;com.vineetmanohar.module.CodeGenerator&lt;/mainClass&gt;

                                            &lt;arguments&gt;

                                                &lt;argument&gt;arg0&lt;/argument&gt;

                                                &lt;argument&gt;arg1&lt;/argument&gt;

                                            &lt;/arguments&gt;

                                        &lt;/configuration&gt;

                                    &lt;/execution&gt;

                                &lt;/executions&gt;

                            &lt;/plugin&gt;

                        &lt;/plugins&gt;

                    &lt;/build&gt;

                &lt;/profile&gt;

            &lt;/profiles&gt;

        02. mvn test -Pcode-generator

    4) Advanced options:

        01. You can get a list of all available parameters by typing:

            mvn exec:help -Ddetail=true -Dgoal=java

 

10\. mvn dependency:copy-dependencies

&lt;plugin&gt;

    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;

    &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;

    &lt;configuration&gt;

        &lt;outputDirectory&gt;m2\_repo&lt;/outputDirectory&gt;

        &lt;overWriteIfNewer&gt;true&lt;/overWriteIfNewer&gt;

        &lt;useRepositoryLayout&gt;true&lt;/useRepositoryLayout&gt;

        &lt;copyPom&gt;true&lt;/copyPom&gt;

    &lt;/configuration&gt;

&lt;/plugin&gt;

 

11\. deploy snapshot/3rd party maven project to nexus:

pom.xml

\------------------------------------------------------------------------------------------

&lt;distributionManagement&gt;

    &lt;repository&gt;

        &lt;id&gt;nexus-releases&lt;/id&gt;

        &lt;name&gt;Nexus142 Release Repository&lt;/name&gt;

        &lt;url&gt;http://10.199.130.142:8080/nexus/content/repositories/releases/&lt;/url&gt;

    &lt;/repository&gt;

    &lt;snapshotRepository&gt;

        &lt;id&gt;nexus-snapshots&lt;/id&gt;

        &lt;name&gt;Nexus142 Snapshot Repository&lt;/name&gt;

        &lt;url&gt;http://10.199.130.142:8080/nexus/content/repositories/snapshots/&lt;/url&gt;

        &lt;uniqueVersion&gt;false&lt;/uniqueVersion&gt; &lt;---- avoid to append current timestamp on uploaded jars.

    &lt;/snapshotRepository&gt;

&lt;/distributionManagement&gt;

 

12\. change default maven repository folder:

${MAVEN\_HOME}\\conf\\settings.xml

\------------------------------------------------------------------------------------------

&lt;settings...&gt;

    &lt;localRepository&gt;D:\\DevTools\\apache-maven-2.2.1\\repo&lt;/localRepository&gt;

&lt;/settings&gt;

&lt;profile&gt;

    &lt;id&gt;nexus&lt;/id&gt;

    &lt;repositories&gt;

        &lt;repository&gt;

            &lt;id&gt;local-nexus&lt;/id&gt;

            &lt;url&gt;http://10.199.130.142:8080/nexus/content/groups/public&lt;/url&gt;

            &lt;releases&gt;

                &lt;enabled&gt;true&lt;/enabled&gt;

                &lt;updatePolicy&gt;never&lt;/updatePolicy&gt;

            &lt;/releases&gt;

            &lt;snapshots&gt;

                &lt;enabled&gt;true&lt;/enabled&gt;

                &lt;updatePolicy&gt;never&lt;/updatePolicy&gt;

            &lt;/snapshots&gt;

        &lt;/repository&gt;

    &lt;/repositories&gt;

&lt;/profile&gt;

 

13\. setting Nexus account:

${MAVEN\_HOME}\\conf\\settings.xml

\------------------------------------------------------------------------------------------

&lt;settings...&gt;

    &lt;servers&gt;

        &lt;server&gt;

            &lt;id&gt;nexus-releases&lt;/id&gt;

            &lt;username&gt;admin&lt;/username&gt;

            &lt;password&gt;mmis@123&lt;/password&gt;

        &lt;/server&gt;

        &lt;server&gt;

            &lt;id&gt;nexus-snapshots&lt;/id&gt;

            &lt;username&gt;admin&lt;/username&gt;

            &lt;password&gt;mmis@123&lt;/password&gt;

        &lt;/server&gt;

    &lt;/servers&gt;

&lt;/settings&gt;

 

14\. run specified testcase in maven-test:

mvn -Dtest=MenuItemFacadeTest test

 

15\. repository list:

&lt;repositories&gt;

    &lt;repository&gt;

      &lt;id&gt;maven2-repo1&lt;/id&gt;

      &lt;name&gt;Maven2 Repo 1&lt;/name&gt;

      &lt;url&gt;http://repo1.maven.org/maven2&lt;/url&gt;

    &lt;/repository&gt;

    &lt;repository&gt;

      &lt;id&gt;ibiblio&lt;/id&gt;

      &lt;name&gt;iBiblio Maven2 Repository&lt;/name&gt;

      &lt;url&gt;http://www.ibiblio.org/maven2&lt;/url&gt;

    &lt;/repository&gt;

    &lt;repository&gt;

      &lt;id&gt;apache-repo&lt;/id&gt;

      &lt;name&gt;Apache Repository&lt;/name&gt;

      &lt;url&gt;http://people.apache.org/repo/m2-snapshot-repository&lt;/url&gt;

    &lt;/repository&gt;

    &lt;repository&gt;

      &lt;id&gt;compass-project.org&lt;/id&gt;

      &lt;name&gt;Compass&lt;/name&gt;

      &lt;url&gt;http://repo.compass-project.org&lt;/url&gt;

    &lt;/repository&gt;

    &lt;repository&gt;

      &lt;id&gt;java.net&lt;/id&gt;

      &lt;name&gt;JavaNet&lt;/name&gt;

      &lt;url&gt;http://download.java.net/maven/2&lt;/url&gt;

    &lt;/repository&gt;

    &lt;repository&gt;

      &lt;id&gt;http://maven.seasar.org/maven21&lt;/id&gt;

      &lt;name&gt;http://maven.seasar.org/maven2&lt;/name&gt;

      &lt;url&gt;http://maven.seasar.org/maven2&lt;/url&gt;

    &lt;/repository&gt;

&lt;/repositories&gt;