user253202
user253202

Reputation:

Maven plugin complex parameter initialization via system properties

I need to run maven plugin from console. In plugin i need a complex parameter kind of:

/**
* @goal do-automation
**/
public class AutomationMojo extends AbstractMojo {

/**
 * The current maven project.
 *
 * @parameter expression="${project}"
 */
protected MavenProject project;

/**
 * App configuration.
 *
 * @parameter expression="${appConfig}"
 */
private AppConfig appConfig;

AppConfig parameter looks smth like this:

public class AppConfig {

private String path

private String version

}

I will be running maven plugin in the following way:

mvn group:artifact:version:do-automation -Dproperty.for.appConfig

How can i set AppConfig properties via system properties? It is possible?

i tried the following and it didn't work for me:

public class AppConfig {
/**
* @parameter expression="${path}"
*/
private String path

private String version

}

mvn group:artifact:1.0-SNAPSHOT:do-automation -DappConfig.path=122 -Dpath=122 It created AppConfig with null values for properties

I am using: Apache Maven 2.2.1 (r801777; 2009-08-06 14:46:01-0430) Java version: 1.6.0_21 Java home: c:\Program Files\Java\jdk1.6.0_21\jre Default locale: en_US, platform encoding: Cp1252 OS name: "windows 7" version: "6.1" arch: "x86" Family: "windows"

Upvotes: 2

Views: 1905

Answers (3)

tgrigoryan
tgrigoryan

Reputation: 349

Actually Maven 3 provides some other means to do what you want. Please, have a look to this link, section (Bean Default Properties)

http://blog.sonatype.com/people/2011/03/configuring-plugin-goals-in-maven-3/

You can define set(String configStr) method in your AppConfig and parse string passed from command line. For Instance.

mvn group:artifact:1.0-SNAPSHOT:do-automation -DappConfig=my/path,version

Then you will be able to parse "my/path,version" in the set(...) method appropriately.

Upvotes: 4

khmarbaise
khmarbaise

Reputation: 97567

you should change your configuration like the following:

/**
 * App configuration.
 *
 * @parameter
 */
private AppConfig appConfig;


public class AppConfig {
/**
* @parameter expression="${appConfig.path}"
*/
private String path

/**
* @parameter expression="${appConfig.version}*/
private String version

}

This should give the opportunity to use your system configuration arguments. First i would check if the configuration via usual configuration tag for a plugin works as expected to see if something different is wrong.

Upvotes: 0

khmarbaise
khmarbaise

Reputation: 97567

you have read this already. Here is an excerpt:

configuration @parameter expression="${aSystemProperty}" default-value="${anExpression}"

Specifies the expressions used to calculate the value to be injected into this parameter of the Mojo at buildtime. The expression given by default-value is commonly used to refer to specific elements in the POM, such as ${project.resources}, which refers to the list of resources meant to accompany the classes in the resulting JAR file. Of course, the default value need not be an expression but can also be a simple constant like true or 1.5. And for parameters of type String one can mix expressions with literal values, e.g. ${project.artifactId}-${project.version}-special. The system property given by expression enables users to override the default value from the command line via -DaSystemProperty=value. NOTE: If neither default-value nor expression are specified, the parameter can only be configured from the POM. The use of '${' and '}' is required to delimit actual expressions which may be evaluated.

Upvotes: 1

Related Questions