Reputation: 1509
I'm trying to enclose some legacy code in a maven plugin. The legacy code is writing its outputs in the OS current working directory. I want to move all that into the target dir. I.e. if my project nesting is
A
-B
-C
and I run mvn install in B or C, I get the outputs in B or C and want to move them to B/target and C/target. Even worse, if I launch mvn install in A, outputs for both B and C wind up in A, which is obviously very bad (overwrites).
I've looked for a way to get the current project folder (not the place mvn install was launched from), but have found nothing so far that doesn't involve pushing this all back at the user (command line hacks, pom hacks, properties files, etc). I've also wished for a way to change the working directory before launching the legacy, but Java doesn't allow that. So how should I proceed?
Upvotes: 6
Views: 7052
Reputation: 1862
Just a quick note: for latest maven plugins, annotations could be used instead:
@Parameter(readonly = true, defaultValue = "${project}")
private MavenProject project;
In newer versions of maven based on docs here: https://maven.apache.org/plugin-tools/maven-plugin-tools-annotations/index.html. It can just be annotated with @Component like so:
@Component // since Maven 3.2.5, thanks to MNG-5695
private MavenProject project;
Upvotes: 10
Reputation: 1509
To answer my own question yet again (happens to me a lot), the solution starts here:
/**
* The maven project.
*
* @parameter expression="${project}"
* @readonly
*/
private MavenProject project;
Then in a setup method:
this.model = project.getModel();
this.build = model.getBuild();
this.finalName = build.getFinalName();
this.targetDir = new File(build.getDirectory());
which gets targetDir pointing at the right place. Then the legacy code runs, dropping dots wherever it pleases and the plugin cleans up by MOVING them into target dir.
That was good enough for starters. But with targetDir available, I actually wound up toilet training the legacy code to do its business in the right place.
Upvotes: 8
Reputation: 1790
If you only want to get the directory where the artifact is built it might be a bit neater to do this:
/**
* @parameter expression="${project.build.directory}/${project.build.finalName}"
* @readonly
*/
private File outputPath;
Upvotes: 0