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=<archetype-groupId> \
-DarchetypeArtifactId=<archetype-artifactId> \
-DarchetypeVersion=<archetype-version> \
-DgroupId=<my.groupid> \
-DartifactId=<my-artifactId>
1. setting resource files:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
2. compiler plugin:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
3. tomcat plugin(mvn tomcat:run):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.0-beta-1</version>
<configuration>
<path>/trsWeb</path>
<port>9080</port>
</configuration>
</plugin>
4. jetty plugin(mvn jetty:run):
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.24</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<contextPath>/trsWeb</contextPath>
<connectors>
<connector implementation="org.mortbay.jetty.bio.SocketConnector">
<port>9080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
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):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>package</id>
<phase>package</phase>
<configuration>
<tasks if="${boolVariable}">
<property name="compile_classpath" refid="maven.compile.classpath"/>
<property name="runtime_classpath" refid="maven.runtime.classpath"/>
<property name="test_classpath" refid="maven.test.classpath"/>
<property name="plugin_classpath" refid="maven.plugin.classpath"/>
<echo message="compile classpath: ${compile_classpath}"/>
<echo message="runtime classpath: ${runtime_classpath}"/>
<echo message="test classpath: ${test_classpath}"/>
<echo message="plugin classpath: ${plugin_classpath}"/>
<copy file="d:/1.txt" tofile="d:/2.txt"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
or: mvn antrun:run
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.4</version>
<configuration>
<tasks if="${build.prod}">
<property name="basedir" value="${basedir}" />
<property name="compile_classpath" refid="maven.compile.classpath" />
<property name="runtime_classpath" refid="maven.runtime.classpath" />
<property name="test_classpath" refid="maven.test.classpath" />
<property name="plugin_classpath" refid="maven.plugin.classpath" />
<property name="build.dir" value="${project.build.directory}" />
<property name="dist" value="${build.dir}/dist" />
<property name="dist.src" value="${dist}/src" />
<ant antfile="${basedir}/m2-build.xml">
<target name="${build.task}" />
</ant>
</tasks>
</configuration>
</plugin>
6. profiles.xml
<?xml version="1.0" encoding="UTF-8"?>
<profilesXml>
<profiles>
<profile>
<id>development</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<maven.test.skip>false</maven.test.skip>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>development</activeProfile>
</activeProfiles>
</profilesXml>
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:
<activation>
<property>
<name>env</name>
<value>was</value>
</property>
</activation>
2. define the default ${env} property in pom.xml:
<properties>
<env>dev</env>
</properties>
3. don't specify any activiate profile in neither pom.xml nor profiles.xml
4. mvn package -Denv=was
8. encoding resource copying:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
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" <------- no args
03. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2" -Dexec.commandlineArgs="-Xloggc:<file>" <------- with args
04. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime <------- with runtime dependencies in classpath
2) Running in a phase in pom.xml
01. <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.vineetmanohar.module.CodeGenerator</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
02. mvn test
3) Running in a profile in pom.xml
01. <profiles>
<profile>
<id>code-generator</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.vineetmanohar.module.CodeGenerator</mainClass>
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
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
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<outputDirectory>m2_repo</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
<useRepositoryLayout>true</useRepositoryLayout>
<copyPom>true</copyPom>
</configuration>
</plugin>
11. deploy snapshot/3rd party maven project to nexus:
pom.xml
------------------------------------------------------------------------------------------
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus142 Release Repository</name>
<url>http://10.199.130.142:8080/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus142 Snapshot Repository</name>
<url>http://10.199.130.142:8080/nexus/content/repositories/snapshots/</url>
<uniqueVersion>false</uniqueVersion> <---- avoid to append current timestamp on uploaded jars.
</snapshotRepository>
</distributionManagement>
12. change default maven repository folder:
${MAVEN_HOME}\conf\settings.xml
------------------------------------------------------------------------------------------
<settings...>
<localRepository>D:\DevTools\apache-maven-2.2.1\repo</localRepository>
</settings>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>local-nexus</id>
<url>http://10.199.130.142:8080/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
</profile>
13. setting Nexus account:
${MAVEN_HOME}\conf\settings.xml
------------------------------------------------------------------------------------------
<settings...>
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>mmis@123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>mmis@123</password>
</server>
</servers>
</settings>
14. run specified testcase in maven-test:
mvn -Dtest=MenuItemFacadeTest test
15. repository list:
<repositories>
<repository>
<id>maven2-repo1</id>
<name>Maven2 Repo 1</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>ibiblio</id>
<name>iBiblio Maven2 Repository</name>
<url>http://www.ibiblio.org/maven2</url>
</repository>
<repository>
<id>apache-repo</id>
<name>Apache Repository</name>
<url>http://people.apache.org/repo/m2-snapshot-repository</url>
</repository>
<repository>
<id>compass-project.org</id>
<name>Compass</name>
<url>http://repo.compass-project.org</url>
</repository>
<repository>
<id>java.net</id>
<name>JavaNet</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>http://maven.seasar.org/maven21</id>
<name>http://maven.seasar.org/maven2</name>
<url>http://maven.seasar.org/maven2</url>
</repository>
</repositories>