本文转载自http://shift-alt-ctrl.iteye.com/blog/1939783
mvn package-DskipTests 打包并且跳过测试
一. 常用指令
1) 生成eclipse项目: mvn clean eclipse:clean eclipse:eclipse
如果构建项目时需要下载依赖包的源文件,需要在此后追加"-DdownloadSources=true"
2) 部署到本地库: mvn clean source:jar install -Dmaven.test.skip=true
source:jar表示生成源文件(*-source.jar)
3) 通过指令生成maven项目: mvn archetype:create -DgroupId=com.test.demo -DartifactId=test
如果要生成一个web项目,需要在此后追加"-DarchetypeArtifactId=maven-archetype-webapp"
二.常用插件
1) compiler: 编译源文件
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.1</version>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
可以通过"mvn compiler:compiler"来编译,[参考]
2) resources: 打包时将resource文件拷贝到classpath下
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <!-- 部署jar时,将会排除exclude下的文件进入jar,在web项目中,通常将properties文件排除在jar之外,而是放在classes文件中(参见war插件) -->
- <excludes>
- <exclude>*.properties</exclude>
- </excludes>
- <filtering>true</filtering>
- </resource>
- </resources>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
3) jar: 将项目打包成jar,也可以是"可运行的java"程序
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <manifest>
- <!-- 是否将项目的classpath添加到manifest-->
- <addClasspath>true</addClasspath>
- <!-- 指定main方法 -->
- <mainClass>com.test.MainClass</mainClass>
- </manifest>
- </archive>
- </configuration>
- </plugin>
可以通过"mvn jar:jar"运行打包为jar文件,[参考]
4) war: 将项目打包为war
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.4</version>
- <configuration>
- <!-- 是否将classes文件部署成jar包 -->
- <archiveClasses>true</archiveClasses>
- <webResources>
- <resource>
- <!-- 相对pom.xml的目录 -->
- <directory>src/main/resources/</directory>
- <!-- 文件目标位置 -->
- <targetPath>WEB-INF/classes</targetPath>
- <includes>
- <include>*.xml</include>
- <include>*.properties</include>
- </includes>
- <!--
- <excludes>
- <exclude>**/image2</exclude>
- </excludes>
- -->
- </resource>
- </webResources>
- </configuration>
- </plugin>
可以通过"mvn war:war"来生成文件,[参考]
5) source: 生成source文件
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-source-plugin</artifactId>
- <version>2.2.1</version>
- <configuration>
- <!-- 相对当前的输出路径 ,默认在target下
- <outputDirectory>target/</outputDirectory>
- -->
- <!-- 生成的文件名称,默认为"<build>.<fileName>-source.jar"
- <finalName>tttt.jar</finalName>
- -->
- <attach>false</attach>
- </configuration>
- </plugin>
可以通过"mvn source:jar"等指令运行,可以通过追加"javadoc:javadoc"生成javadoc文件,[参考]
6) eclipse/IDEA:
可以通过"mvn eclipse:eclipse" 或者"mvn idea:idea"生成相应的工程,[参考]
三. 将项目发布到nexus上.
1) 在maven的setting.xml中新增服务
- <servers>
- <server>
- <id>nexus-releases</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- <server>
- <id>nexus-snapshots</id>
- <username>admin</username>
- <password>admin123</password>
- </server>
- </servers>
- <!-- 项目依赖包库 -->
- <profiles>
- <profile>
- <id>dev</id>
- <repositories>
- <repository>
- <id>nexus</id>
- <url>http://192.168.0.132:8080/nexus/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>nexus</id>
- <url>http://192.168.0.132:8080/nexus/content/repositories/central/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </pluginRepository>
- </pluginRepositories>
- </profile>
- </profiles>
- <activeProfiles>
- <activeProfile>dev</activeProfile>
- </activeProfiles>
2) pom.xml文件中指定发布目标位置:
- <distributionManagement>
- <repository>
- <id>nexus-releases</id><!-- 和setting.xml对应 -->
- <url>http://192.168.0.132:8080/nexus/content/repositories/releases/</url>
- </repository>
- </distributionManagement>
使用"mvn source:jar deploy"即可;建议将开发过程中的jar发布到snapshot中,同时如果nexus设定了"Disable reploy"(不允许重复发布),将会导致同一个版本的项目不能被多次发布(400错误).