开发者

Filter dependencies copied by Maven's copy-dependency?

开发者 https://www.devze.com 2023-03-01 17:48 出处:网络
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 depend

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&g开发者_C百科t;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>


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号