Vitali Plagov
Vitali Plagov

Reputation: 762

Copy specific artifacts with dependencies to the specific folder and rest of dependencies to another folder

I have a maven project with 5 dependencies. Two of them (with its transitive dependencies) I need to have in a custom location, let's say in client/plugins. While rest of the project dependencies (also with transitive dependencies) I need in another location, let's say in client/modules. Following examples from Apache Maven documentation, here's my <build> block:

<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.0.2</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <artifactItems>
                <artifactItem>
                    <groupId>my.groupid</groupId>
                    <artifactId>my-artifactid-one</artifactId>
                    <version>1.0-SNAPSHOT</version>
                    <outputDirectory>client/modules</outputDirectory>
                </artifactItem>
                <artifactItem>
                    <groupId>my.groupid</groupId>
                    <artifactId>my-artifactid-one</artifactId>
                    <version>1.1-SNAPSHOT</version>
                    <outputDirectory>client/modules</outputDirectory>
                </artifactItem>
            </artifactItems>
            <outputDirectory>client/plugins</outputDirectory>
        </configuration>
    </plugin>
  </plugins>
</build>

When I run mvn -U clean package command, Maven copies all project dependencies with its transitive dependencies to client/plugins. Thus, it ignores configurations I've set up for specific artifacts.

How can I set it up?

Upvotes: 4

Views: 2640

Answers (1)

Vitali Plagov
Vitali Plagov

Reputation: 762

Looks like it is not possible to achieve this by using just one goal. So I defined two copy-dependencies goals. Then specified different output directories and used excludeArtifactIds parameter to exclude artifacts that are not needed. It looks like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <id>copy-plugins</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>client/plugins</outputDirectory>
                        <excludeArtifactIds>module-artifact-one, module-artifact-two </excludeArtifactIds>
                    </configuration>
                </execution>
                <execution>
                    <id>copy-modules</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>client/modules</outputDirectory>
                        <excludeArtifactIds>plugin-artifact-one, plugin-artifact-two</excludeArtifactIds>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I run mvn -U clean package and it works as I expect now.

Upvotes: 1

Related Questions