创建java项目:
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=myProject
创建web项目:
mvn archetype:create -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.mycompany.app -DartifactId=myProject
增加eclipse项目信息:
cd myProject
mvn eclipse:eclipse
编译:
mvn compile
运行测试:
mvn test
打包(java项目打出jar;web项目打出war):
mvn package
安装jar到本地仓库:
mvn install
清除生成的文件:
mvn clean
========================================================
War 简单介绍
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <!-- 释放将项目的类文件打成jar放到lib目录中。 打成jar的好处是:只修改class时,可以只更新jar。 --> <archiveClasses>true</archiveClasses> <webResources> <!-- src/main/resources是默认的资源目录。 当archiveClasses=true时,resources中的文件资源会被打到jar中; 当archiveClasses=false时,resources中的文件资源在war的根目录。 --> <resource> <!-- config作为source folder,不会被打到jar中。 --> <directory>src/main/config</directory> <targetPath>WEB-INF/classes</targetPath> <filtering>true</filtering> </resource> </webResources> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> </manifest> <manifestEntries> <Built-On>${timestamp}</Built-On> </manifestEntries> </archive> </configuration> <executions> <execution> <id>generate-manifest</id> <phase>prepare-package</phase> <goals> <goal>manifest</goal> </goals> </execution> </executions> </plugin>
需要的属性配置:
<properties> <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format> <timestamp>${maven.build.timestamp}</timestamp> </properties>
========================================================
Assembly打多个自定义包
首先了解两个参数:构建目录/最终项目构建名称
${project.build.directory}/${project.build.finalName}
对应target\myProject
打多个包
每个包有自己的执行器,都绑定到某个生命周期上,有各自的描述符文件。
Pom中配置如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <executions> <execution> <id>make-jar</id><!-- 随便一个名字 --> <phase>package</phase><!-- 绑定到package生命周期阶段上 --> <goals> <goal>single</goal><!-- 只运行一次 --> </goals> <configuration> <skipAssembly>false</skipAssembly><!-- 跳过这个执行器 --> <!-- 自定义输出目录(finalName为最终项目构建名称) --> <!-- <outputDirectory> ${project.build.directory}/${project.build.finalName}/WEB-INF/lib </outputDirectory> --> <!-- 描述符文件 --> <descriptors> <descriptor>src/main/assembly/jar.xml</descriptor> </descriptors> </configuration> </execution> <execution> <id>make-zip</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <skipAssembly>true</skipAssembly> <descriptors> <descriptor>src/main/assembly/zip.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin>
jar.xml文件
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>jar-release</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>${project.build.directory}/classes</directory> <includes> <include>**</include> </includes> <excludes> <exclude>server.properties</exclude> </excludes> <outputDirectory>/</outputDirectory> </fileSet> </fileSets> </assembly>
zip.xml文件
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>release</id> <formats> <format>zip</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>${project.build.directory}/${project.build.finalName}</directory> <includes> <include>**</include> </includes> <outputDirectory>/project</outputDirectory> </fileSet> </fileSets> </assembly>