2

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>
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
user3637541
  • 675
  • 4
  • 15
  • Why do you need to write to `src/META-INF` ? Why no using `src/main/resources/META-INF` and let the resources plugin do it's work ? What kind of purpose is this for? – khmarbaise Sep 03 '16 at 08:49
  • I am writing a code generator. The generator is capable of taken handwritten code (and config) into account. The `pom.xml` i have the problem with is for a maven-project which takes a model and generates code from it and then compiles the generated code (while taking into account the hand written code). The generated code is put into `gen/`, the handwritten code is expected to be in `src`. I want to use default-configuration-files if none are given. That is why i want to extract them only if not present. – user3637541 Sep 04 '16 at 23:20

1 Answers1

0

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)
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • Thanks for your answer. It kind of solves my problem. I am working on a code-generator that adds files into the /src/ directory if they are not present (they are present if the user chooses to use hand-written files and not the generated ones). If i go with your approach i had to use one profile for each file (because one file can be present and the other one not, i don't wont to overwrite both of them, just add the missing one). Do you know a more elegant solution to this problem? – user3637541 Sep 02 '16 at 15:53
  • @user3637541 did you find a better way? the question still shows up as unanswered in the global list and in general I was curious to know if you got any better solution – A_Di-Matteo Sep 16 '16 at 21:54
  • sadly not, i went with your solution. – user3637541 Sep 24 '16 at 13:28
  • In a general description of the plugin this option (`overWrite`) was presented (https://maven.apache.org/plugins/maven-dependency-plugin/usage.html): ... [ true or false ] # this one ... and it works fine for me. – Ivan Trechyokas Jan 18 '23 at 20:06