Maven copy JAR generated in Target to external folder
Asked Answered
C

2

8

In regular Netbeans project you have a build.xml where I am able to copy the generated JAR file to a different folder(s) using the following:

<target name ="-post-jar" > 
    <copy file ="${dist.jar}" todir ="../Plugin Jars" failonerror ="true"/> 

    <copy file ="${dist.jar}" todir ="/Users/dev/Desktop/plugins" failonerror ="true"/> 
</target>

Now that I have a project that uses Maven, I can't find the way to accomplish the same.

EDIT: Thanks to our contributors I was able to do it with the following entry

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.0.1</version>
        <executions>
          <execution>
            <id>copy-installed</id>
            <phase>install</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>${project.groupId}</groupId>
                  <artifactId>${project.artifactId}</artifactId>
                  <version>${project.version}</version>
                  <type>${project.packaging}</type>
                </artifactItem>
              </artifactItems>
              <outputDirectory>/Users/dev/Desktop/plugins</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
Caseous answered 28/7, 2017 at 16:40 Comment(4)
I think you can accomplish that by using copy-dependencies goal like this: <goals> <goal>copy-dependencies</goal> </goals>Ogden
where should put the path?Caseous
This is the full code: <executions> <execution> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions>Ogden
So you have now bound your build with your local configuration of folder structure and operation system...Mazurek
L
4

Use the maven-dependency-plugin

The following documentation has an example where they copy the just built artifact to a custom location.

https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html (search the page for "The dependency:copy goal can also be used to copy the just built artifact to a custom location if desired")

Lobby answered 28/7, 2017 at 16:58 Comment(1)
Thanks @Lobby I will update my post with the proper code that you suggestCaseous
T
0

@Deadron answer is correct. I just copy the correct code snippet here (with small improvements) for better reference.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.6.1</version>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <artifactItems>
            <artifactItem>
                <groupId>${project.groupId}</groupId>
                <artifactId>${project.artifactId}</artifactId>
                <version>${project.version}</version>
                <type>${project.packaging}</type>
                <overWrite>true</overWrite>
                <outputDirectory>/my/install/path</outputDirectory>
                <destFileName>MyDestJar.jar</destFileName>
            </artifactItem>
        </artifactItems>
    </configuration>
</plugin>
Thundery answered 18/4 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.