Search code examples
javamavenmaven-3maven-dependency-plugin

Maven-dependency-plugin: overWrite = false ignored


My problem is that i want to extract files from a .jar file with maven but only if the files do not exist in the output directory. So if i have a file /src/META-INF/beans.xml then i only want the persistence.xml extracted, etc.

Sadly the maven-plugin irgnores all combinations with <overWrite>false</overWrite> that i tried.

Question: Any idea what i am doing wrong? Is it possible?

<build>
  ...
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
      <execution>
        <id>unpack</id>
        <phase>validate</phase>
        <goals>
          <goal>unpack</goal>
        </goals>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId> ... </groupId>
              <artifactId> ... </artifactId>
              <version>${project.version}</version>
              <outputDirectory>${basedir}/src/META-INF</outputDirectory>
              <includes>beans.xml,persistence.xml</includes>
              <overWrite>false</overWrite>
            </artifactItem>
          </artifactItems>
          <overWriteIfNewer>false</overWriteIfNewer>
          <overWrite>false</overWrite>
          <overWriteSnapshots>false</overWriteSnapshots>
          <overWriteReleases>false</overWriteReleases>
        </configuration>
      </execution>
    </executions>
  </plugin>
  ...
</build>

Solution

  • The simple overWrite property actually doesn't exist as a property of the plugin, hence it is simply ignored by the plugin.

    Moreover, you are writing to ${basedir}/src/META-INF, your project, which is probably not the best option, but in some cases could still be reasonable.

    You hence want to write it only once, during the validate phase of the project and most probably only during the first build: that is, if exists, don't override it again.

    for these requirements the following may suit better:

    • Use a Maven profile
    • Use the activation option for files missing: that is, if the file is not there (first time ever) activate the profile which will execute your plugin configuration; when the file is already there, don't activate the profile, no action obviously won't override anything

      <profiles>
        <profile>
          <id>unpack-files</id>
          <activation>
            <file>
              <missing>${basedir}/src/META-INF/beans.xml</missing>
            </file>
          </activation>
          <build>
             <plugins>
                 <!-- move here the maven-dependency-plugin unpack execution -->
             </plugins>
          </build>
        </profile>
      </profiles>
      

    As such you would still have full control of this logic:

    • It will be activated when the file is not there, first execution
    • It can still be activated on demand, via its id (mvn clean install -Punpack-files)