0

I'd like to delete the content of my entire repository (.m2/repository) before the installation phase. Of course I don't want to do it by hand so I am looking for a plugin which does the magic. So far I came across maven-clean-plugin and I am trying to use it as follows:

<build>
      <sourceDirectory>src/</sourceDirectory>
      <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
               <source>${jdk.version}</source>
               <target>${jdk.version}</target>
            </configuration>
        </plugin>  
        <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
        <filesets>
                  <fileset>
                      <directory>${settings.localRepository}/</directory>
                      <includes>
                          <include>**/*</include>
                      </includes>
                  </fileset>
        </filesets>
        </configuration>
        <executions>
          <execution>
            <id>auto-clean</id>
            <phase>install</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      </plugins>
   </build>

I expect this to wipe out the entire repository before downloading the new artifacts, and finally to remove the target folder from the modules. Removal of target folders works, however the wiping out the repository is kinda not working. It does wipe out the repository, however then maven complains about some artifacts required are missing so the compilation fails and returns such errors:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.3:resources (default-resources) on project com.google.protobuf: Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.3:resources failed: Plugin org.apache.maven.plugins:maven-resources-plugin:2.3 or one of its dependencies could not be resolved: Could not find artifact org.apache.maven.plugins:maven-resources-plugin:jar:2.3 -> [Help 1]

I feel like I am pretty close to the solution. Probably I just need to tweak the parameter tags of the plugin.

Could anyone give an idea?

Schütze
  • 1,044
  • 5
  • 28
  • 48
  • I'm curious: Why do you want to do this? – J Fabian Meier Jul 28 '16 at 08:38
  • Because sometimes for some reason it keeps the old setup and leads to compilation errors. – Schütze Jul 28 '16 at 09:00
  • Maybe it would be better to investigate _why_ these errors happen because usually it is not necessary to delete the local repository in every build. Redownloading the dependencies should lead to exactly the same files, only SNAPHSOT dependencies can have newer versions. Those could be drawn with the -u option. – J Fabian Meier Jul 28 '16 at 09:12

1 Answers1

5

If you clean the entire local repository you also delete all plugins which are needed by maven and was downloaded before clean runs. You should use the dependency plaugin for delteing only jars which are dependecies of your Project:

mvn dependency:purge-local-repository

In pom you can use it like:

  <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <version>2.7</version> 
    <executions> 
      <execution> 
        <id>purge-local-dependencies</id> 
        <phase>clean</phase> 
        <goals> 
          <goal>purge-local-repository</goal> 
        </goals> 
        <configuration> 
          <resolutionFuzziness>groupId</resolutionFuzziness> 
          <includes> 
            <include>org.ambraproject</include> 
          </includes> 
        </configuration> 
      </execution> 
    </executions> 
  </plugin> 
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Is there a way to incorporate this into my plugin? I am sure there is a tag to say delete only those. – Schütze Jul 28 '16 at 08:50
  • @Schütze No the clean plugin only deletes files/directories and do not know if it is a plugin or not – Jens Jul 28 '16 at 08:53
  • I think you didn't understand me. I already have the plugin code as I shared, and I want to tell this plugin to delete only the artifacts, not those necessary files. And I am asking if we can specify this in the tag. Like this, `**/*.jar` – Schütze Jul 28 '16 at 08:59
  • Yes i think there will be an exclude tag where you can add all plugins you Need. But why not using the dependency plugin? – Jens Jul 28 '16 at 09:02
  • Well, that's also fine, but do you have a sample usage of the case? I mean the plugin code snippet where I can try? – Schütze Jul 28 '16 at 09:05
  • @Schütze you mean foe the exclude in clean plugin? – Jens Jul 28 '16 at 09:06
  • Dependency plugin which purges the local repository without deleting the core files required for maven to run – Schütze Jul 28 '16 at 09:07
  • Does it do it before the installation? All I want is to run `mvn install` and let this script to clean the repo first, and then download whatever is necessary. Also, is there a way to include all projects without listing them one by one? – Schütze Jul 28 '16 at 09:12
  • @Schütze It run in clean Phase. I do not think so. – Jens Jul 28 '16 at 09:13
  • @Schütze try to Change initialize. i think it will then run when you type mvn install – Jens Jul 28 '16 at 09:15
  • Hmm well, I get `Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.10:purge-local-repository (purge-local-dependencies) on project blabla.mw: Failed to refresh project dependencies for: blabla:blabla.mw:bundle:1.0.0.qualifier: required artifacts missing:` , not sure why the heck would it require project to delete projects. – Schütze Jul 28 '16 at 09:27