1、mvn对于不同的jar包配置。Jar、War、Tar、Zip
jar包的pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration> <!-- 插件配置 -->
<archive> <!-- 存档配置 -->
<addMavenDescriptor>false</addMavenDescriptor><!--生成的jar中,不要包含pom.xml和pom.properties这两个文件,true表示包含 -->
<manifest> <!-- 具体配置 -->
<addClasspath>true</addClasspath> <!--是否要把第三方jar放到manifest的classpath中-->
<classpathPrefix>lib/</classpathPrefix> <!--生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/ -->
<!--应用的main class -->
<mainClass>com.main.Hello</mainClass>
</manifest>
</archive>
<excludes> <!--过滤掉不希望包含在jar中的文件-->
<exclude>${project.basedir}/xml/*</exclude>
</excludes>
<includes> <!--只包含在jar中的文件-->
<include>**/*.class</include>
</includes>
</configuration>
</plugin>
war包pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource><!--打包lib文件夹的jar包-->
<directory>${project.basedir}/lib</directory>
<targetPath>WEB-INF/lib</targetPath>
<filtering>false</filtering>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
打war包时,出现Error assembling WAR: webxml attributeis required需要创建/src/webapp/WEB-INF/web.xml文件。
war是对动态web工程的打包,jar是对java项目的打包。
tar.gz使用加assembly插件
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/distribution.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
distribution.xml如下:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>release</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/resources/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
<files>
<file>
<source>src/main/resources/content.properties</source>
<outputDirectory>conf/content.properties</outputDirectory>
<filtered>true</filtered>
</file>
</files>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
标签<fileSets>中的<directory>src/main/resources/bin</directory>指明要对src/main/resources/bin文件夹操作,<outputDirectory>bin</outputDirectory>指明经过assembly插件打包后bin目录会放置在项目根目录下,
<fileMode>0755</fileMode>指明bin目录下所有文件的权限为755。
assembly插件不仅可以对文件夹操作,同样也可以对单独文件操作。
mvn archetype:generate |
创建maven项目 |
mvn package -Prelease |
打包,并生成部署用的包 |
mvn eclipse:eclipse |
生成eclipse项目文件 |
mvn eclipse:clean |
清除eclipse项目文件 |
mvn site |
生成项目相关信息的网站 |
mvn compile |
编译,将Java 源程序编译成 class 字节码文件 |
mvn test |
测试,并生成测试报告 |
mvn clean |
将以前编译得到的旧的 class 字节码文件删除 |
mvn pakage |
打包,动态 web工程打 war包,Java工程打 jar 包 |
mvn install |
将项目生成 jar 包放在仓库中,以便别的模块调用 |
mvnarchetype:generate 创建项目
-DgroupId=com.mycompany.app 设置groupId=com.mycompany.app
-DarchetypeArtifactId=maven-archetype-quickstart创建项目maven插件maven-archetype-plugin中的原型标识名
-DinteractiveMode=false 交互模式,默认为true。我上面的cmd命令是一次性把该输入的都输入进去了,免去了交互所以是false。如果你选择交互的话,就是有一步一步的选项指引你去选择和填写相关的标识之类的。
例2:mvn clean install -Dmaven.test.skip -Denv=release
-Dmaven.test.skip 跳过测试
-Denv=release 使用-D传递环境变量,其中evn对应设置的<name>值,release对应<value>