Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to copy dependency sources to a directory in maven

Tags:

java

maven

jar

I know that with mvn dependency:sources one can download all dependencies' sources. With mvn copy-dependencies one can download all dependencies into a specified local directory.

How to combine the two so that I can copy all dependencies' sources to a directory?

like image 506
Qiang Li Avatar asked May 10 '16 21:05

Qiang Li


2 Answers

you can simply use

mvn dependency:copy-dependencies -Dclassifier=sources

It will download all source jars linked to the dependencies of your project and copy them in yourProject/target/dependency folder

The optional parameter "classifier" of the maven-dependency-plugin is documented here https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html

like image 103
smasper Avatar answered Oct 06 '22 00:10

smasper


I would not use any of those solutions.

Just include the maven-dependeny-plugin to your maven build and adjust the configuration to your needs:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.10</version>
  <configuration>
    <outputDirectory>/tmp/alternateLocation</outputDirectory>
  </configuration>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>none</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>/tmp/alternateLocation</outputDirectory>
        <overWriteReleases>false</overWriteReleases>
        <overWriteSnapshots>false</overWriteSnapshots>
        <overWriteIfNewer>true</overWriteIfNewer>
      </configuration>
    </execution>
  </executions>
</plugin>

You can change several items to suit your needs, for example, in the example I'm giving you, which I think directly gives you a solution, I've specified that in no phase are the dependencies to be copied the alternateLocation in the tmp folder. However I'm also saying that my new goal is copy-dependencies. So in the command line this would be something like:

mvn dependency:copy-dependencies

If you notice I now have the outputDirectory configured twice. Inside the execution it means that it will only be considered when you are running a specified maven build phase like packaging, clean, test, ... . As a first child sibling of the plugin node, it means that it will be considered when the command line explicitly invoques the dependency plugin, which is what you want.

You can find more about the maven-dependency plugin here:

  • https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html

Since you need the dependencies and the sources at the same time, the best way I can think of is by running maven normally without an implicit call to the actual plugin. If your run it through the post-clean phase (i.e. mvn post-clean) it will run the two following goals:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <configuration>
        <outputDirectory>/tmp/alternateLocation</outputDirectory>
    </configuration>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>post-clean</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>/tmp/alternateLocation</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
        <execution>
            <id>sources</id>
            <phase>post-clean</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <classifier>sources</classifier>
                <outputDirectory>/tmp/alternateLocation</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>

It will always make the copy to the destination folder, but won't overwrite if the files are already there. I had to choose a non frequently used phase. post-clean seemed to be a best candidate here. This is only thinking that I want to isolate this kind of build. post-clean also cleans up the build. If you want just to keep using this plugin everytime you make a build then I would recommend putting it on the clean or maybe the install phase. That way it always happens on the background and you don't worry about it.

like image 29
Joao Esperancinha Avatar answered Oct 05 '22 23:10

Joao Esperancinha