delete the 'target' directory after build - maven

i know this is probably frowned upon by maven lovers, but the whole 'target' directory is a waste of space in the context of our program and it's deployment. we have other build processes responsible for creating the actual deployment and i currently manually delete the target dir after every maven build, so that its contents don't interfere with my file searches etc...
is there a way to delete this dir automatically at the end of a maven build/install?
thanks, p.

Use the maven-clean-plugin as here http://maven.apache.org/plugins/maven-clean-plugin/examples/delete_additional_files.html
<project>
[...]
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>install</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>

You should simply add the clean goal to your maven goals at the end.
mvn install clean
The problem with the clean-plugin is that if you like to run the clean at the end of the build it depends which goal you called at the beginning. For example you called mvn package you need to have a phase post-package which does not exist or if you called mvn install you have to have phase post-install which does not exist either.

The easiest way is simply to add "clean" to the end of the normal build command. eg. mvn clean install clean.

if you want to just delete some directories in target folder, you have to create some construct like this.
this for instance deletes just all contents of folders:
target/unpack
gen-external-apklibs
excludeDefaultDirectories allows to not delete complete target folder.
i used it to clean up target folder before lint analysis.
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>Deleting all unnecessary files before lint analysis</id>
<phase>verify</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>target/unpack</directory>
<followSymlinks>false</followSymlinks>
<excludes>
<exclude>*</exclude>
</excludes>
</fileset>
<fileset>
<directory>gen-external-apklibs</directory>
<followSymlinks>false</followSymlinks>
<excludes>
<exclude>*</exclude>
</excludes>
</fileset>
</filesets>
<verbose>true</verbose>
</configuration>
</plugin>

Related

How to Add JUnit Results to Tycho Built JAR

We are using Maven and sometimes Tycho, and I want to put the results of the JUnit tests into the resulting JARs.
With Maven, that's easy. I just added the following to the pom.xml:
<resources>
<resource>
<directory>target/</directory>
<includes>
<include>surefire-reports/*.*</include>
</includes>
</resource>
</resources>
The test phase is before the package phase, so all is well.
For Tycho however, tests are executed after the package phase (because they are executed in the verify phase for some reason). The phase cannot be changed either (see bug 440094).
So the only alternative is to build another JAR after the verify phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>post-integration-test</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>verification-doc</classifier>
<includes>
<include>**/surefire-reports/*.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
However, it does not work. Neither the folder nor the files are added. My guess was that it's because of the include tag points to the source folders, so I tried:
<include>${project.build.outputDirectory}/../surefire-reports/*.*</include>
<include>${project.build.directory}/surefire-reports/*.*</include>
Which did not work either?
So how do I add JUnit test results to the JAR of a Tycho build?
You can change the directory of the maven-bundle-plugin like this:
<configuration>
<classesDirectory>${project.build.directory}</classesDirectory>
<includes>
<include>surefire-reports/*</include>
</includes>
</configuration>

Clear local maven repository via maven-plugin on install phase

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?
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>

clean a folder contents during pre-integration-test phase

I have parent pom where I'm trying to unpack some scripts, execute them inside and in "pre-integration-test" phase, so it runs by default for all child modules.
My problem here is I need to delete the contents of certain directory each time it runs. I tried using ant-plugin which never runs in the pre-integration-phase. Also to note I'm calling several profiles while building the project.
mvn clean install -Pprofile1,profile2,integration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>pre-integration-test</phase>
<configuration>
<tasks>
<delete>
<fileset dir="checkout\myproject\specific_directory\**.*"/>
<delete/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
Overall I have four plugins including ant clean all running in pre-integration-phase. Except ant clean up task all others run correctly.
Based on the documentation and on my personal experience i assume you configured the plugin in the wrong area. Furthermore have you called mvn via:
mvn verify
to execute integration-test phase.
<build>
[...]
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>cleanup</id>
<phase>pre-integration-test</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>some/relative/path</directory>
<includes>
<include>**/*.tmp</include>
<include>**/*.log</include>
</includes>
</fileset>
</filesets>
</configuration>
</execution>
<executions>
</plugin>
[...]
</build>

Maven-release-plugin and builder-helper plugin

I guess i am missing something.. I want to make a release of my project. I added maven-release-plugin to my pom. In addition, i have another source code dir aside from java(call it gen-src). When i make the first steps in the maven release (i.e prepare) everything is ok, but when i make perform it does not take the gen-src in account.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/gen_src</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
I suspect that it may be connected to the fact that the phase is generated-sources. Do i need to attach the add-source goal to another phase? If yes, how?
I also read in here - this is similar problem though i am not using flex..no answer.
Any idea?Thanks.
I had the same problem in the past and I think I know what's happening. The release:perform phase checkouts a copy of the tag to be released to 'target/checkout' folder and fork a maven process to build this checkout. As you have a problem only in the release:perform phase, it must be related to the fact that maven in running a fork process on the 'target/checkout' folder, not in the './' folder.
I ended up fixing this problem removing build helper, but I don't know if you could do the same, so if I were you I would try avoiding relative paths on configurations. You can configure the build-helper like that:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/main/gen_src</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Defining explicitly the ${basedir} could avoid this problem because ${basedir} will resolve to the fork path (your_workspace/project/target/checkout) instead of the current path (your_workspace/project). If this doesn't fix the problem, I believe we should file a bug against build-helper-maven-plugin because there should be no errors only in the perform phase.
The generated source classes won't make it into the binary jar — there you'll find only .class files resulting from compilation of both regular and generated sources. Maven release plug-in will, though, include the extra source directory into sources jar.
There is no need to execute "add-source" goal in any other phase; what you could find useful is letting the clean plug-in know that it should include the extra directory:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<filesets>
<fileset>
<directory>${basedir}/src/main/gen_src</directory>
<includes>
<include>**/*</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>

Maven clean plugin exclude .cvsignore

In my project I originally made a mistake and committed the target directory in the cvs repository; I know there is no safe way to remove a directory from CVS, so I put a .cvsignore file there to basically ignore everything (I don't want developers who aren't able to even merge properly to commit their classes...).
The problem raises with my Jenkins CI, because I run clean and test goals; basically clean is run before CVS update, so it always finds a file to update (the .cvsignore that has been wiped by clean) and triggers an often useless build.
I think the way to go is to use exclusions but I tried and did not work:
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting file set: **************************/target (included: [**], excluded: [])
The exclusion is configured as:
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<executions>
<execution>
<id>not-clean</id>
<configuration>
<filesets>
<fileset>
<directory>target</directory>
<excludes>
<exclude>*cvsignore</exclude>
</excludes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
You could try setting <excludeDefaultDirectories> to true, Otherwise, I believe target folder would always get deleted.
The following code snippet works for me. Note that I have used default-clean as the id.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>default-clean</id>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>target</directory>
<excludes>
<exclude>.cvsignore</exclude>
<exclude>CVS</exclude>
<exclude>CVS/**</exclude>
</excludes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>

Resources