How to remove generated build artifacts from Maven's target directory? Maven generates a jar or war file to target directory. I'd like to remove that file after maven has installed the jar/war file to local repository (that is, after maven has executed the 'install' goal). The remove could happen either at install goal or separate goal I execute manually.
Note, that I'd like leave other parts of target directory intact, for example target/site and target/surefire-reports.
Just use the clean plugin and run an execution after the install phase:
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>install</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<filesets>
<fileset>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>**/*.jar</include>
</includes>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
There is nothing built into Maven that can do this. You could use the antrun plugin to execute an Ant script after install that deletes the artifact, or use the exec plugin to use the command line to delete the artifact, or write your own plug-in.
I suggest there is little value, if any, in doing any of these things. Maven is designed to place intermediate and final artifacts in target to make follow-on builds more efficient. The reason that there is nothing available to do this already is an indicator that this is of little value. If it is of value to you, you have a few options.
I know I am a little bit late. But I guess the issue was, that a maven project archives the artifacts automatically. In my case, I disabled the automatic archiving and just archived the artifacts manually using the post build actions. This way, only the artifacts that I am interested in are archived. I am willing to leave the generated artifacts on disk until the next build runs.
Related
How do I tell Maven to also publish the SQL artifact for the DBA?
Here's the thing: when we release every new version of our Maven application, we need to publish two artifacts:
The web application (e.g. app-1.2.0.war file) -- for the WebSphere guy.
The database changes for this version (e.g. dba-1.2.0.sql file) -- for the DBA.
The SQL changes file is currently src/main/database/dba.sql, but I can change that dir or file name if necessary.
As of now Maven publishes the war artifact automatically (mvn clean deploy) to the artifact repository, and that's perfect. However, I wanted it to publish the SQL file at the same time, in the same command as well... and it doesn't.
How can I do that?
I see that we can tell Maven to publish extra artifacts (e.g. sources, javadoc) at once, so I guess it should be possible to publish SQL files as well, but this is just a guess.
You can use the Build Helper plugin for that.
But the file name is computed from artifactid, version, type and classifier.
If you need to absolutely push a different name with a different artifactId, you will need either to mvn deploy:deploy-file ... (from a command in your CI or with an ant script in the pom) or create an additional pom file and launch maven against it.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>src/main/database/dba.sql</file>
<type>sql</type>
<!-- <classifier>xxx</classifier> -->
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
link to the source: https://www.mojohaus.org/build-helper-maven-plugin/usage.html
I got a liferay-portlet-project with a sample application/portlet that I want to become an archetype. Inside the project there is a folder containing two *.launch files to redeploy the webapp. Both have the following line which I have trouble with:
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${workspace_loc:/rawportlet}"/>
where "rawportlet" is the project's name. If I change it manually to ${artifactId} this variable is not resolved when using the archetype to create a project. Resolving this variable during project-generation would be nice.
Is there a way to achieve this? Or a workaround? Thanks in advance for your help.
Workaround: write a maven goal that the user can run after using the archetype. So the steps would be (for example):
generate project from archetype
mvn archetype:generate -DarchetypeCatalog=local
do some post-generation cleanup (execute in project's base dir)
mvn antrun:run
So my code for this is in "pom.xml" in the archetype:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<replace token= "rawportlet" value="${artifactId}" dir="runConfigs">
<include name="**/*.launch"/>
</replace>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
The "runConfigs" directory is where the *.launch files are stored.
Credits to:
Full search and replace of strings in source files when copying resources
Maven, configure specific goal
I have this same problem, and I used a different solution that works okay (but isn't perfect either).
Use value="${workspace_loc}/${artifactId}" in your launch config.
This will work as long as people do an archetype:gen at the workspace root. This works better for me than the selected answer because running that post processing requires another launch configuration (which somewhat defeats the whole purpose).
The package phase of a project with packaging war, prepares an exploded-war in the target folder and packages this into the final war-file.
Is it possible to make some changes, editing files, removing files and so on, between prepare-package and package phases? I'm searching for an extension point (Maven execution-phase) where the resources are already copied and in the exploded-war structure.
[maven phase] Copy resources and explode to target/{finalName}.
[custom] Do some complex custom changes (e.g. implemented with maven-antrun).
[maven phase] Package the changed stuff into the final war.
I thought this could be possible between the phases prepare-package and package. Unfortunately after the prepare-package no exploded war is available to be changed and packaged later.
Can you give me a hint how to achieve this? Thank you very much.
This configuration calls the exploded goal in the prepare-package phase. This gives you the chance to work on the exploded war directory in subsequent plugin definitions e.g. maven-antrun.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
</plugin>
It sound to me like you should bind the antrun task to the prepare package phase, because at this point the resources have already been processed see Lifecycle Reference.
I am trying to figure out how to aggregate my maven dependencies in a multi-module project. For example, if I have:
root pom/project1
root pom/project2
and I run mvn dependency:copy-dependencies, I end up with the dependencies in:
root pom/project1/target/dependency
root pom/project2/target/dependency
What I really want is that if I run the mvn command in the root pom folder, all of the dependencies to be copied to root pom/dependency. Is there a maven property that gets me the output directory of the root pom? (similar to ${project.build.directory})? I realize that I can just copy all the dependency folders to the same place after the fact, but I was hoping for something a little cleaner.
You will have to configure the dependency plugin to copy depdendencies to a particular location. This can be done by the outputDirectory configuration property.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${outputDir}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
But if you trying to do this for distribution, I'd recommend you create an assembly using the maven assembly plugin
The documentation says:
The Assembly Plugin for Maven 2.0 is primarily intended to allow users to aggregate the
project output along with its dependencies, modules, site documentation, and other files
into a single distributable archive.
What I really want is that if I run the mvn command in the root pom folder, all of the dependencies to be copied to root pom/dependency. Is there a maven property that gets me the output directory of the root pom? (similar to ${project.build.directory})?
No, because modules shouldn't actually be aware of that.
I realize that I can just copy all the dependency folders to the same place after the fact, but I was hoping for something a little cleaner.
The Maven way would to use the Maven Assembly Plugin and a custom descriptor. But if you're not familiar with the Maven Assembly Plugin and its descriptor format, it won't be easy.
A less clean but easier approach would be to configure the Maven Dependency plugin to copy the dependencies into the parent project using a relative path. Something like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<configuration>
<outputDirectory>../root_pom/target/dependency</outputDirectory>
</configuration>
</plugin>
But as I said, this introduces tight coupling between modules and the root project which is not good at all (and I wouldn't go further by including the goal invocation as part of the build, modules should remain independent and you should be able to build one module without "checkouting" the parent).
On a CI build server, the local Maven repository fills up the file system repetitively (after a few days).
What strategy are others doing to trim the local repository in such a case?
-Max
The Maven dependency plugin has a purge-local-repository goal that allows you to delete the dependencies for a given project from the local repository, if this is run say once a day on each project the snapshots will not accumulate.
Alternatively there's a more scorched-earth approach you could take. As the problem is typically the timestamped snapshot artifacts, you could use the maven-antrun-plugin to delete all files that match the resource collection pattern.
For example (note this might need some tweaking as I've done it from memory):
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<tasks>
<delete>
<fileset dir="${settings.localRepository}">
<include name="**/*.jar"/>
<exclude name="**/*.pom"/>
<exclude name="**/*.war"/>
<exclude name="**/*.ear"/>
<exclude name="**/*.md5"/>
<exclude name="**/*.sha"/>
<!--any other extensions?...-->
<!--match the timestamp pattern-->
<containsregexp expression="[0-9]{8}.[0-9]{6}-[0-9]+"/>
</fileset>
</delete>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
If you're using hudson, you can set up a scheduled job to just delete the entire repository once a day or something like that. I've got a job called hudson-maven-repo-clean which has this configuration:
Build / Execute shell: rm -rf ~hudson/.m2/repository
Build Triggers / Build periodically: 0 0 * * *
In addition to purge-local-repository (which reads to me like a nuclear option, as it only offers an excludes configuration as opposed to an explicit includes), take a look at the Remove Project Artifact mojo. I'm looking to implement it now, as my exact use case is to clear out large WAR and EAR snapshots that are being built on my CI (and sometimes workstation) machines.
We use especially for this purpose the build-helper plugin. In our company parent pom is the remove-project-artifact goal embedded in the profile for our hudson builds. This way all old versions of this artifact are removed prior to installing the currently build version.
...
<profile>
<id>hudson</id>
<activation>
<property>
<name>BUILD_TAG</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>remove-old-artifacts</id>
<phase>package</phase>
<goals>
<goal>remove-project-artifact</goal>
</goals>
<configuration>
<removeAll>true</removeAll>
</configuration>
</execution>
</executions>
</plugin>
...
Using removeAll set to true will wipe out all other snapshots except the one your working on. This can be dangerous as it may mean snapshots for a branch will be wiped out as well.
For instance if you have a snapshot 1.0.0.18-SNAPSHOT representing HEAD and snapshot 1.0.1.17-SNAPSHOT representing a branch, running this plugin with 1.0.0.18-SNAPSHOT build will wipe the 1.0.1.17-SNAPSHOt folder.
To get around this scenario the removeAll should be set to false.
We have employed a slightly different (and devious) technique. All artifacts that build "large things" (EARs, WARs, TARs) have their deploy location overriden like so:
<properties>
<discard-me-in-bit-bucket>file://${basedir}/target/_DELETEME</discard-me-in-bit-bucket>
</properties>
<distributionManagement>
<repository>
<id>upload-InternalSite</id>
<name>SoftwareLibrary External</name>
<url>${discard-me-in-bit-bucket}</url>
<layout>legacy</layout>
<uniqueVersion>false</uniqueVersion>
</repository>
<snapshotRepository>
<id>upload-InternalSite</id>
<name>Repository Name</name>
<url>${discard-me-in-bit-bucket}</url>
<layout>legacy</layout>
<uniqueVersion>false</uniqueVersion>
</snapshotRepository>
</distributionManagement>
This strategy causes the deploy goal to put things in the target directory, which of course is destroyed by the next CLEAN operation. To get even more aggressive, we have a postbuild step that does this:
find -type d -name '*_DELETEME' -exec rm -rf '{}' ';' -prune || echo $?
We employ yet one more strategy, too. In Hudson/Jenkins we provide a settings file to place the .m2 repository in the workspace for the job. This allows us to delete the entire repository before or after the job. It also makes artifacts visible in the workspace which aids in debugging some problems.
How big is the file system? We have 10gb allocated to builds and zap snapshots older than 30 days every night. That seems to work
Are you doing builds every X hours or when code changes? Switching to code changes will reduce the number of artifacts without reducing coverage.
Are you installing all snapshots locally? You don't need to do this in all cases. In most cases, just those snapshots that are actively developed dependancies need to be installed locally.
Are you installing EAR/WAR files locally? You probably don't need them either.
How many workspaces are you keeping? We use hudson and keep only the last 5 builds.