原博客:http://ljhzzyx.blog.163.com/blog/static/3838031220138335540882/
mvn dependency:copy-dependencies可以准备好当前项目依赖的jar包,可以配置文件中scope为test的jar也包含进来了。生产系统发布的时候是不需要测试用的jar包的,如junit相关的包。想直接使用命令来指定copy的jar包的scope,可是貌似maven命令中没有提供这样的方式,网上也没找到例子。需要在pom.xml文件中做配置,配置参数在这里
在profile里面配置
<profile>
<id>compile-jar</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<configuration>
<outputDirectory>${lib.dir}</outputDirectory>
<includeScope>compile</includeScope>
</configuration>
</plugin>
</plugins>
</build>
</profile>
通过-P参数来激活
mvn dependency:copy-dependencies -P compile-jar
这样就可以只copy compile级别的jar包了。
一开始把
configuration配到了
executions的内部,这样不生效。
configuration要与
executions同级,其内部的
includeScope这样的属性才有效。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<excludeScope>test</excludeScope>
<excludeArtifactIds>junit,dbunit,mockito-all</excludeArtifactIds>
<excludeTransitive>true</excludeTransitive>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
不配置直接使用命令行是这样
mvn dependency:copy-dependencies -DincludeScope=compile
不过compile scope还有问题,比如
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
这时
provided级别的jar包也会复制过去,有的时候会出错。比如
javaee-web-api的jar包部署到weblogic中,会报找不到jsf相关的jar包找不到的错误,部署不成功(tomcat不会)。所以运行的项目使用runtime的scope才合适。具体各个scope的含义,插件官网有说明:
runtime : scope gives runtime and compile dependencies,
compile : scope gives compile, provided, and system dependencies,
test (default) : scope gives all dependencies,
provided : scope just gives provided dependencies,
system : scope just gives system dependencies.
上面所举的例子中,有个参数<excludeTransitive>true</excludeTransitive>,默认是false。如果是true则只复制pom文件中定义了的jar,这样一般不符合需求,所以一般不用配置excludeTransitive参数。profile这种方式也不是必须的,configuration要与executions同级,则像下面这样配置也行
<plugin>
<configuration>
<excludeScope>provided</excludeScope>
<excludeArtifactIds>junit,dbunit,mockito-all</excludeArtifactIds>
<excludeTransitive>false</excludeTransitive>
<includeScope>runtime</includeScope>
</configuration>
<executions>
......
</executions>
</plugin>
各个参数的详细含义见:
http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html