过滤由Maven复制依赖复制的依赖? [英] Filter dependencies copied by Maven's copy-dependency?

查看:73
本文介绍了过滤由Maven复制依赖复制的依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上需要完成以下工作:

I need to essentially accomplish the following:

  1. 将我的库构建为JAR. (容易,已经完成.)
  2. 将库的依赖项复制到本地文件夹(包括主项目JAR)中,不包括标记为provided的依赖项.
  1. Build my library into a JAR. (Easy, already done.)
  2. Copy my library's dependencies to a local folder, including the main project JAR, excluding dependencies marked as provided.

我似乎无法完成第二部分.是否有比我在下面做的更好的方法?我实质上是将这些JAR部署到服务器上的lib目录中.不幸的是,下面的代码包括所有JAR,甚至包括provided,但不包括项目输出JAR.我应该为此使用其他插件吗?

I can't seem to get the second part finished. Is there a better way to do this than how I'm doing it below? I'm essentially deploying these JARs to a lib directory on a server. Unfortunately, the code below includes all JARs, even provided ones, but doesn't include the project output JAR. Should I be using a different plugin for this?

<?xml version="1.0"?>
<project>
    ...

    <dependencies>
        <dependency>
            <groupId>com.provided</groupId>
            <artifactId>provided-lib</artifactId>
            <version>1.2.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>

        ...

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>/hello</outputDirectory>
                            <excludeTransitive>true</excludeTransitive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

推荐答案

为防止插件收集提供的依赖关系,您可以使用@Raghuram解决方案(该解决方案为+1).我还尝试跳过 test 范围内的依赖项,并发现了问题不可能做到那么简单-插件语义中的 test 意思是"一切".

To prevent the pluging to collect provided dependencies you can use @Raghuram solution (+1 for that). I tried also to skip test scoped dependencies and found the issue that it can not be done that simple - as test means 'everything' in the plugin semantic.

因此排除提供的 test 范围的解决方案是包括Scope runtime .

So the solution to exclude provided and test scope is to includeScope runtime.

<includeScope>runtime</includeScope>

收集依赖项后,您可以使用 maven-antrun-plugin复制项目jar 到目标目录,例如:

After collecting the dependencies you can copy the projects jar with the maven-antrun-plugin to the target directory, e.g.:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${java.io.tmpdir}/test</outputDirectory>
                        <includeScope>runtime</includeScope>                        
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <configuration>
                        <tasks>
                        <copy
                            file="${build.directory}/${project.artifactId}-${project.version}.jar"
                            todir="${java.io.tmpdir}/test" />
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我不知道任何其他解决方案-除了创建一个新的 pom-dist.xml (也许是<packaging>pom</packaging>)之外,它仅将依赖项保存到您的库并收集所有传递性依赖项(不包括测试/提供)范围.如果您不想提供一个全新的项目,则可以使用mvn -f pom-dist.xml package 执行.

I do not know any other solution - beside creating a new pom-dist.xml (maybe <packaging>pom</packaging>) which just holds the dependency to your library and collects all transitive dependencies exclusive test/provided scope. You can execute this with mvn -f pom-dist.xml package if you do not want to provide a whole new project.

这篇关于过滤由Maven复制依赖复制的依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆