maven 只复制compile级别的jar包

     

原博客: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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值