1

I have created a maven profile with maven-dependency-plugin inside it.

Below is my plugin

         <profile>
            <id>copy-dep</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                        <execution>
                            <id>copy-external</id>
                            <phase>none</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                        </execution>
                        </executions>
                        <configuration>
                            <excludeGroupIds>group ids that I need to exclude</excludeGroupIds>
                            <excludeArtifactIds>artifact ids that I need to exclude</excludeArtifactIds>
                            <includeArtifactIds>artifact ids that I need to include</includeArtifactIds>
                            <includeGroupIds>group id that I need to include</includeGroupIds>
                            <outputDirectory>${project.build.directory}/libs</outputDirectory>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

I am using the below command to execute

mvn dependency:copy-dependencies -DoutputDirectory=libs -Pcopy-dep

But when I execute the command it looks for all the dependencies defined in pom and copies them as well.

I have tried to put unwanted dependencies inside exclude tag but it didn't work then I also tried by removing exclude tag and keeping the required dependencies but also didn't worked.

In my pom, I am using maven assembly plugin to separate out the required dependencies, which I don't want to get copied with the created profile.

Any idea where I am going wrong here? is there a better way to achieve the same.

Kashyap
  • 385
  • 3
  • 13

2 Answers2

1

To copy only "listed" artifacts (in this example it will copy only junit and mockito):

<profile>
  <id>copy-dep</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.8</version>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.12</version>
            </artifactItem>
            <artifactItem>
              <groupId>org.mockito</groupId>
              <artifactId>mockito-core</artifactId>
              <version>2.28.2</version>
            </artifactItem>
          </artifactItems>
          <outputDirectory>${project.build.directory}/libs</outputDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

and to execute it:

mvn dependency:copy -Pcopy-dep
Gmugra
  • 468
  • 7
  • 10
  • Thanks for the solution it's working fine, is there a way I can download transitive dependencies as well as also exclude some dependencies. – Kashyap Jun 17 '20 at 13:43
0

You need to tell Maven which execution to run. So write:

mvn dependency:copy-dependencies@copy-external -DoutputDirectory=libs -Pcopy-dep

BTW: Putting this into a profile is probably not necessary.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Thanks for the solution, still it is not working. When I use mvn dependency:copy command it downloads only the dependency mentioned under but not the transitive dependencies. If I use mvn dependency:copy-dependencies command it again looks out for all the dependencies in pom and fails. – Kashyap Jun 17 '20 at 14:30