过滤 Maven 的复制依赖项复制的依赖项?
我基本上需要完成以下任务:
- 将我的库构建到 JAR 中。 (简单,已经完成。)
- 将我的库的依赖项复制到本地文件夹,包括主项目 JAR,不包括标记为
provided
的依赖项。
我似乎无法完成第二部分。有没有比我下面的方法更好的方法来做到这一点?我实质上是将这些 JAR 部署到服务器上的 lib 目录中。不幸的是,下面的代码包含所有 JAR,甚至是提供的
JAR,但不包含项目输出 JAR。我应该为此使用不同的插件吗?
<?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>
I need to essentially accomplish the following:
- Build my library into a JAR. (Easy, already done.)
- Copy my library's dependencies to a local folder, including the main project JAR, excluding dependencies marked as
provided
.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了防止插入收集提供的依赖项,您可以使用@Raghuram解决方案(为此+1)。我还尝试跳过测试范围依赖项并发现问题它不能那么简单地完成 - 因为测试在插件语义中意味着“一切”。
因此,排除提供和测试范围的解决方案是包含运行时范围。
收集依赖项后,您可以使用 maven-antrun-plugin 到目标目录,例如:
我不知道任何其他解决方案 - 除了创建一个新的 pom-dist.xml (也许
pom
),它仅保存对您的库的依赖项并收集所有传递依赖项独占测试/提供的范围。如果您不想提供全新的项目,可以使用mvn -f pom-dist.xml package
执行此操作。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.
So the solution to exclude provided and test scope is to includeScope runtime.
After collecting the dependencies you can copy the projects jar with the maven-antrun-plugin to the target directory, e.g.:
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 withmvn -f pom-dist.xml package
if you do not want to provide a whole new project.如此处所述,您可以尝试设置
excludeScope
参数以排除具有provided
范围的依赖项。至于不包括当前项目jar的插件,我想这是设计使然。
您可以创建一个单独的 Maven 项目来完成这项工作。
As documented here, you could try setting
excludeScope
parameter to exclude dependants withprovided
scope.As for the plugin excluding the current project jar, I guess this is by design.
You could create a separate maven project to do this job.
添加我的两分钱。
将
provided
放入执行中不起作用。要排除提供的 jar,请将元素置于执行之外:
命令:
mvn dependency:copy-dependencies
。jar 被复制到
target/dependency
。Adding my two cents.
Putting the
<excludeScope>provided</excludeScope>
in the execution didn't work.To exclude the provided jars put the element outside of the execution:
Command:
mvn dependency:copy-dependencies
.Jars are copied to
target/dependency
.