项目构建工具Maven

ymc4 10年前

Maven 是一个项目管理和构建自动化工具。但是对于我们程序员来说,我们最关心的是它的项目构建功能。所以这里我们介绍的就是怎样用 maven 来满足我们项目的日常需要。

Maven 使用惯例优于配置的原则 。它要求在没有定制之前,所有的项目都有如下的结构:

目录 目的
${basedir} 存放 pom.xml和所有的子目录
${basedir}/src/main/java 项目的 java源代码
${basedir}/src/main/resources 项目的资源,比如说 property文件
${basedir}/src/test/java 项目的测试类,比如说 JUnit代码
${basedir}/src/test/resources 测试使用的资源

Maven常用命令

mvn archetype:create 创建Maven项目  mvn compile 编译源代码  mvn deploy 发布项目  mvn test-compile 编译测试源代码  mvn test 运行应用程序中的单元测试  mvn site 生成项目相关信息的网站  mvn clean 清除项目目录中的生成结果  mvn package 根据项目生成的jar  mvn install 在本地Repository中安装jar  mvn eclipse:eclipse 生成eclipse项目文件  mvnjetty:run 启动jetty服务  mvntomcat:run 启动tomcat服务  mvn clean package -Dmaven.test.skip=true:清除以前的包后重新打包,跳过测试类

setting.xml

<?xml version="1.0" encoding="UTF-8"?>  <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">   <localRepository>/home/clara/.m2/repository</localRepository>   <servers>    <server>     <id>nexus</id>     <username>${username}</username>     <password>${password}</password>    </server>   </servers>   <mirrors>    <mirror>     <id>nexus</id>     <mirrorOf>*</mirrorOf>     <url>http://192.168.0.159:8080/nexus/content/groups/public/</url>    </mirror>   </mirrors>   <profiles>    <profile>     <id>jdk6</id>     <activation>      <jdk>1.6</jdk>     </activation>     <properties>      <maven.compiler.source>1.6</maven.compiler.source>      <maven.compiler.target>1.6</maven.compiler.target>      <maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>     </properties>    </profile>   </profiles>   <activeProfiles>    <activeProfile>jdk6</activeProfile>   </activeProfiles>  </settings>    #项目pom.xml  #增加以下配置,可以deploy到nexus's releases 或 snapshots      <distributionManagement>    <repository>     <id>nexus</id>     <url>http://192.168.0.159:8080/nexus/content/repositories/releases/</url>    </repository>    <snapshotRepository>     <id>nexus</id>     <url>http://192.168.0.159:8080/nexus/content/repositories/snapshots/</url>    </snapshotRepository>   </distributionManagement>    #增加以下配置,可以deploy到nexus's 3rd party    <distributionManagement>   <repository>    <id>nexus</id>    <url>http://192.168.0.159:8080/nexus/content/repositories/thirdparty/</url>   </repository>  </distributionManagement>

1.设置字符集

<properties>      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>

2.拷贝src/main/resources/资源文件

<build>      <pluginManagement>          <plugins>              <plugin>                  <groupId>org.apache.maven.plugins</groupId>                  <artifactId>maven-resources-plugin</artifactId>                  <version>2.5</version>                  <executions>                      <execution>                          <id>copy-resources</id>                          <phase>package</phase>                          <goals>                              <goal>copy-resources</goal>                          </goals>                          <configuration>                              <encoding>${project.build.sourceEncoding}</encoding>                              <outputDirectory>${project.build.directory}</outputDirectory>                              <resources>                                  <resource>                                      <directory>src/main/resources/</directory>                                      <includes>                                          <include>*.properties</include>                                          <include>*.xml</include>                                      </includes>                                      <filtering>true</filtering>                                  </resource>                              </resources>                          </configuration>                      </execution>                  </executions>              </plugin>          </plugins>      </pluginManagement>  </build>

3.编译代码

<build>      <pluginManagement>          <plugins>              <plugin>                  <groupId>org.apache.maven.plugins</groupId>                  <artifactId>maven-compiler-plugin</artifactId>                  <version>2.5</version>                  <configuration>                      <source>1.7</source>                      <target>1.7</target>                  </configuration>              </plugin>          </plugins>      </pluginManagement>  </build>

4.编译打包成jar文件

<build>      <pluginManagement>          <plugins>              <plugin>                  <groupId>org.apache.maven.plugins</groupId>                  <artifactId>maven-jar-plugin</artifactId>                  <executions>                      <execution>                          <phase>package</phase>                          <goals>                              <goal>jar</goal>                          </goals>                          <configuration>                              <classifier>without-configs</classifier>                              <excludes>                                  <exclude>*.properties</exclude>                                  <exclude>*.xml</exclude>                              </excludes>                          </configuration>                      </execution>                  </executions>              </plugin>      </pluginManagement>  </build>

5.构建测试用例配置

<build>      <pluginManagement>          <plugins>              <plugin>                  <groupId>org.apache.maven.plugins</groupId>                  <artifactId>maven-surefire-plugin</artifactId>                  <version>2.9</version>                  <configuration>                      <skip>true</skip>                      <testFailureIgnore>true</testFailureIgnore>                  </configuration>              </plugin>          </plugins>      </pluginManagement>  </build>

6.输出依赖jar文件到指定目录

<build>      <pluginManagement>          <plugins>              <plugin>                  <groupId>org.eclipse.m2e</groupId>                  <artifactId>lifecycle-mapping</artifactId>                  <version>1.0.0</version>                  <configuration>                      <lifecycleMappingMetadata>                          <pluginExecutions>                              <pluginExecution>                                  <pluginExecutionFilter>                                      <groupId>org.apache.maven.plugins</groupId>                                      <artifactId>maven-dependency-plugin</artifactId>                                      <versionRange>[2.0,)</versionRange>                                      <goals>                                          <goal>copy-dependencies</goal>                                          <goal>unpack</goal>                                      </goals>                                  </pluginExecutionFilter>                                  <action>                                      <ignore />                                  </action>                              </pluginExecution>                          </pluginExecutions>                      </lifecycleMappingMetadata>                  </configuration>              </plugin>          </plugins>      </pluginManagement>      <plugins>          <plugin>              <groupId>org.apache.maven.plugins</groupId>              <artifactId>maven-dependency-plugin</artifactId>              <version>2.8</version>              <executions>                  <execution>                      <id>copy-dependencies</id>                      <phase>package</phase>                      <goals>                          <goal>copy-dependencies</goal>                      </goals>                      <configuration>                          <outputDirectory>${project.build.directory}/lib</outputDirectory>                          <overWriteReleases>false</overWriteReleases>                          <overWriteSnapshots>false</overWriteSnapshots>                          <overWriteIfNewer>true</overWriteIfNewer>                      </configuration>                  </execution>              </executions>          </plugin>      </plugins>  </build>

7.配置指定的repository

<repositories>      <repository>          <id>cloudera</id>          <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>      </repository>  </repositories>

8.将应用及其依赖jar文件打成一个jar文件

<build>      <plugins>          <plugin>              <artifactId>maven-assembly-plugin</artifactId>              <configuration>                  <archive>                      <manifest>                          <mainClass>org.shirdrn.solr.cloud.index.hadoop.SolrCloudIndexer</mainClass>                      </manifest>                  </archive>                  <descriptorRefs>                      <descriptorRef>jar-with-dependencies</descriptorRef>                  </descriptorRefs>              </configuration>              <executions>                  <execution>                      <id>make-assembly</id>                      <phase>package</phase>                      <goals>                          <goal>single</goal>                      </goals>                  </execution>              </executions>          </plugin>      </plugins>  </build>