I need a variable in my archetype resource files that is a path to the directory where the new project is being generated. This StackOverflow post is from a guy who made his own plugin to do this.
Unfortunately, I can’t figure out how it was originally intended to run. If I run this command:
mvn \
com.example.build.maven:property-setter-maven-plugin:0.1:set-properties \
archetype:generate \
-DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DartifactId=...
from an empty directory where I intend to create the new project from the archetype I get this error:
Goal requires a project to execute but there is no POM in this directory
This seems completely wrong to me. I’m trying to create a new maven project in this directory, there shouldn’t already be a pom.xml there. So, I looked up the phases run by maven-archetype-plugin and decided to instead run this plugin on <goal>archetype:generate</goal> and I removed the set-properties from the maven command I’m executing.
Now, when I run the archetype:generate command it generates an archetype, but none of the environment variables that I need exist, its as if the plugin is now doing absolutely nothing.
Does anybody know how I can get this to work?
First the simple answer:
Is the custom plugin really needed? The
${basedir}variable should work in archetype resource files which corresponds to the base directory the archetype was run from.The root of the target project is ${basedir}/${artifactId}, so if my template
pom.xmlis the following:Then the archetype-generated
pom.xmlwill look something like:(assuming I set the groupId, artifactId, version through the command line/prompt)
But now let’s assume you need those environment variables for some other reason, not just the base directory of the project.
From what I can tell, the
property-setter-maven-plugintakes the current execution properties and puts them in environment variables. And you need the base directoryTo get the property-setter-maven-plugin to run, it needs modification since the original requires a project/POM to run as defined in the metadata.
The original MOJO definition:
To make it so this plugin can run without a project present, a couple of changes are needed:
@requiresProject false, because this is true by default. Additional documentation is here.projectfield of typeMavenProjectand it is marked as@parameter required– this needs to be removed so that the MOJO can execute without a project. From a casual glance at the source code from the original post this field was not used so can be safely removed.So you’ll end up with something like:
You can then run the command line as you did before:
and property-setter-maven-plugin should be able to execute now.