Reputation: 6548
I'm working on a custom maven plugin for creating a custom packaging type. I've got it working and can build the zip file in the format I need. However, I've noticed that some configuration elements and variables depend on the name of the zip file.
The zip file has a special manifest file as part of it's format. I want the parameter componentName to be a proper @Parameter
so that other properties can depend on it via ${componentName}
.
I'd like to get the component name dynamically rather than forcing my users to specify it in another place. The manifest file can sometimes contain it. If not, there is usually another file in ${project.basedir}
that will have the same name as the component.
I have a private static final string getComponentName(File baseDir)
defined to calculate the component name when one isn't supplied.
However
@Parameter(property = "componentName",
defaultValue = getComponentName("${project.basedir}"))
protected String componentName;
does not compile with a ParseException: syntax error @[34,46] in file ...
Is there a way to configure this to what I want? If so, how?
Upvotes: 1
Views: 2607
Reputation: 6548
It's probably overkill, however, I figured out a solution.
I wanted to make it a property with a default value so I could make it more easily configurable. including some dependent config properties.
The solution turned out to be adding a new goal to my plugin init
. It runs during the initialize phase. It does the logic to set the componentName
and then runs some additional logic to set the properties for the additional dependent fields, so the later lifecycle phase goals can still use the variables as normal.
Remove the defaultValue from componentName
@Parameter(property = "componentName")
protected String componentName;
Define the Goal:
@Mojo(name = "init", defaultPhase = LifecyclePhase.INITIALIZE)
public class InitMojo extends AbstractComponentMojo
To have it run by default during the lifecycle add the following to the components.xml file:
<initialize>
org.ucmtwine:ucm-maven-plugin:init
</initialize>
Have init
's execute()
do the processing and set the result
public void execute() throws MojoExecutionException, MojoFailureException
{
determineComponentName(); //logic to set componentName
//set the property with the result //not sure which one is truly necessary
project.getProperties().setProperty("componentName", componentName);
session.getUserProperties().setProperty("componentName", componentName);
session.getSystemProperties().setProperty("componentName", componentName);
getLog().debug("Setting componentName: " + componentName);
//force reset of the dependent variables
if ( null == componentFileName || "${componentFileName}".equals(componentFileName) )
{
componentFileName = componentName + ".zip";
project.getProperties().setProperty("componentFileName", componentFileName);
session.getUserProperties().setProperty("componentFileName", componentFileName);
session.getSystemProperties().setProperty("componentFileName", componentFileName);
getLog().debug("Setting componentFileName: " + componentFileName);
}
// more variables get reset here
}
Upvotes: 0
Reputation: 97437
The defaultValue in a annotation can't be a method cause the injection will set the default value and evaluate some kind of java code there..So you have to go the following path:
@Parameter ( defaultValue = "${project.basedir}", property="componentName")
private String componentName;
Furthermore if you need to have a getter which needs to do some than you should simply make the attribute private as i did and use a getter to access it where you can do supplemental things you like..
The values for defaultValue
can be looked up here where you can see what you can use as default values. Furthermore i would suggest not to put something in the ${project.basedir}
cause everything which will be packaged should be located into src/main/...
so if you have something which will not being part of your package it should be located src/Supplemental/
instead...
Upvotes: 2