1、根本观点
项目仅依赖于项目内的pom.xml,不依赖于本地maven配置,达到打开项目无需配置maven即可使用的需求。
2、具体配置
1)、仓库和私服地址统一配置在父项目pom中,不建议配置在maven的setting.xml中,减少项目与maven配置信息的依赖
【根pom.xml配置如下信息】:
<repositories>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>interval:1</updatePolicy>
</releases>
</repository>
<repository>
<id>sourceforge-releases</id>
<name>sourceforge-releases</name>
<url>https://oss.sonatype.org/content/repositories/sourceforge-releases</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://nexus.company.com/nexus/content/groups/public</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
2)、包版本统一在父项目中配置,避免多个子项目配置不同版本的包
【根pom.xml配置如下信息】:
<properties>
<!--配置包版本信息等-->
<lib.util.version>2016.05.24.1</lib.util.version>
<java.version>1.8</java.version>
<junit.version>4.12</junit.version>
<mockito-all.version>1.10.19</mockito-all.version>
<powermock-api-mockito.version>1.6.2</powermock-api-mockito.version>
<powermock-module-junit4.version>1.6.2</powermock-module-junit4.version>
<dom4j.version>1.6.1</dom4j.version>
</properties>
<dependencyManagement>
<dependencies>
<!--inner reference begin-->
<!--inner reference end-->
<!--department inner reference start-->
<dependency>
<groupId>com.company.dealer.lib</groupId>
<artifactId>util</artifactId>
<version>${lib.util.version}</version>
</dependency>
<!--department inner reference end-->
<!-- Unit Test begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito-all.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock-api-mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock-module-junit4.version}</version>
<scope>test</scope>
</dependency>
<!-- Unit Test end -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>${dom4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
【子项目pom.xml配置如下信息】:
<dependencies>
<!--inner reference begin-->
<dependency>
<groupId>com.company.dealer.lib</groupId>
<artifactId>util</artifactId>
</dependency>
<!--inner reference end-->
<!--test reference begin-->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<!--无需输入版本号,版本有父项目的pom来配置-->
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
</dependency>
<!--test reference end-->
</dependencies>
PS:无需指定软件版本信息,默认会继承父节点dependencyManagement节点下的版本信息
3)、插件版本统一在父项目中配置
例如:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<!--bug fix:http://stackoverflow.com/questions/26281322/mavenfailed-to-execute-goal-org-apache-maven-pluginsmaven-resources-plugin2-7-->
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<configuration>
<encoding>utf-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>dat</nonFilteredFileExtension>
<nonFilteredFileExtension>json</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
<!-- 单元测试覆盖率插件 -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
3、示例代码
地址:http://pan.baidu.com/s/1qXJU1UO
来源:oschina
链接:https://my.oschina.net/u/2352002/blog/704230