1

Hello awesome community, I'd like to use your help once more by understanding how can I copy file during maven package phase. For example, Lets say I create a jar called myCustom.jar and I need to copy it to:

  1. ${basedir}\firstLocation
  2. ${basedir}\secondLocation

As for now I'm using maven-dependency-plugin like this:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
    <execution>
        <id>copy</id>
        <phase>package</phase>
        <goals>
            <goal>copy</goal>
        </goals>
        <configuration>
            <artifactItems>
                <artifactItem>
                    <groupId>com.company.group</groupId>
                    <artifactId>artifact-id</artifactId>
                    <version>${project.version}</version>
                    <type>jar</type>
                    <outputDirectory>${basedir}/firstLocation</outputDirectory>
                    <destFileName>myCustom.jar</destFileName>
                </artifactItem>
                <artifactItem>
                    <groupId>com.company.group</groupId>
                    <artifactId>artifact-id</artifactId>
                    <version>${project.version}</version>
                    <type>jar</type>
                    <outputDirectory>${basedir}/secondLocation</outputDirectory>
                    <destFileName>myCustom.jar</destFileName>
                </artifactItem>
                ...
                ...
            <artifactItems>
        <configuration>
    <execution>
<executions>

This solution works but I will end up with an endless spaghetti pom which is a nightmare to maintain. I tried the following:

<artifactItem>
<groupId>com.company.group</groupId>
<artifactId>artifact-id</artifactId>
<version>${project.version}</version>
<type>jar</type>
<outputDirectory>${basedir}/firstLocation</outputDirectory>
<outputDirectory>${basedir}/secondLocation</outputDirectory>
<destFileName>myCustom.jar</destFileName>

But it didn't copy anything (There were no failures in log either :)

Any idea how can I achieve this goal and keep my pom as short and clear as possible?

user2656851
  • 1,643
  • 4
  • 12
  • 15

1 Answers1

1

Generally speaking, you should not use Maven to do this. Maven is a build tool and not a general purpose deployment / maintenance tool.

If I was facing this issue I would do one of the following (in order of preference):

  • Pick one folder to be the destination and convert all the other folders to be symlinks to that particular folder. Than configure to copy the file to the choosen folder.
  • Use a build shell script instead of calling Maven directly. The script will call mvn once and copy the result to all folders.
  • Use a scripting language plugin. For example groovy-maven-plugin allows to inline a groovy script. The script can do the copying then.
Milen Dyankov
  • 2,972
  • 14
  • 25