过滤 Maven 的复制依赖项复制的依赖项?

发布于 2024-11-04 03:07:41 字数 1896 浏览 13 评论 0原文

我基本上需要完成以下任务:

  1. 将我的库构建到 JAR 中。 (简单,已经完成。)
  2. 将我的库的依赖项复制到本地文件夹,包括主项目 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:

  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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绮烟 2024-11-11 03:07:41

为了防止插入收集提供的依赖项,您可以使用@Raghuram解决方案(为此+1)。我还尝试跳过测试范围依赖项并发现问题它不能那么简单地完成 - 因为测试在插件语义中意味着“一切”。

因此,排除提供测试范围的解决方案是包含运行时范围。

<includeScope>runtime</includeScope>

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

<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 (也许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.

<includeScope>runtime</includeScope>

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>

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.

耳钉梦 2024-11-11 03:07:41

此处所述,您可以尝试设置 excludeScope 参数以排除具有 provided 范围的依赖项。

<excludeScope>provided</excludeScope>

至于不包括当前项目jar的插件,我想这是设计使然。
您可以创建一个单独的 Maven 项目来完成这项工作。

As documented here, you could try setting excludeScope parameter to exclude dependants with provided scope.

<excludeScope>provided</excludeScope>

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.

紫轩蝶泪 2024-11-11 03:07:41

添加我的两分钱。

provided 放入执行中不起作用。

要排除提供的 jar,请将元素置于执行之外:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <configuration>
        <excludeScope>provided</excludeScope>
    </configuration>
</plugin>

命令: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:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <configuration>
        <excludeScope>provided</excludeScope>
    </configuration>
</plugin>

Command: mvn dependency:copy-dependencies.

Jars are copied to target/dependency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文