Issue
I am trying to make a jar of a project and then copy it to a target folder but it is not working. Below is the POM. 'Maven Install' does create the jar but it doesn't copy it to the lib folder.
pom.xml
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>/apps/dislocation/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Solution
Plugins just declared in <pluginManagement>
do nothing. This is just a configuration what to do when the plugin is declared elsewhere (in the same or in a module/child POM). You have to have a declaration in <build>/<plugins>
somewhere, too:
However, this only configures plugins that are actually referenced within the
plugins
element in the children or in the current POM.
dependency:copy-dependencies says:
Goal that copies the project dependencies from the repository to a defined location.
That means dependency:copy-dependencies
doesn't copy the JAR you create in the project but all that's declared under <dependencies>
.
Furhermore, the prepare-package
phase comes long before the install
phase, even the JAR is only created in the next phase (package
) so there wouldn't be anything to copy from the repository – if it were the proper choice – at the prepare-package
phase (though there can be an older version of the JAR from a previous mvn install
).
So, you might think to add to your POM:
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
but this gives:
[FATAL] 'dependencies.dependency.[<groupId>:<artifactId>:<version>]'
for <groupId>:<artifactId>:<version> is referencing itself. @ line ..., column ...
So, you have to use one of those in Best practices for copying files with Maven.
Answered By - Gerold Broser
Answer Checked By - Katrina (JavaFixing Volunteer)