开发者

Are there other default maven archetype properties

I'm creating a maven archetype that has a bunch of custom properties.

ex:

<requiredProperties>
  <requiredProperty key="db-name">
    <defaultValue>Some db-name</defaultValue>
  </requiredProperty>
  <requiredProperty key="station-name">
    <defaultValue>loca开发者_如何学Clhost</defaultValue>
  </requiredProperty>
  ...
</requiredProperties>

When a new project is generated based on this archetype maven knows some default variables like groupId, artifactId, version. Does maven knows others trivial variables like env.user, user, host, path, basedir or any others? which are? how can i get them?

thanks.


I realise this is an old question, but I came across this question earlier and up voted it. I was wondering the same, I have now come up with a solution / workaround to enabling access to these properties.

I created my own Maven plugin aptly named property-setter-maven-plugin that sets System and, most importantly, execution properties. The configuration of this plugin allows any number of properties and values to be specified (and since they are defined in a POM, have access to all of the normal variables). When the archetype plugin is then run (in the same Maven execution as my custom plugin), it reads the execution properties and finds any that you have configured.

My Maven command looks something like this:

mvn \
    com.example.build.maven:property-setter-maven-plugin:0.1:set-properties \
    archetype:generate \
    -DarchetypeGroupId=... -DarchetypeArtifactId=... -DarchetypeVersion=... -DartifactId=...

The configuration in the POM that sits in the same directory that the archetype is generated from looks like this:

...
<plugin>
    <groupId>com.example.build.maven</groupId>
    <artifactId>property-setter-maven-plugin</artifactId>
    <version>0.1</version>
    <executions>
        <execution>
            <goals><goal>set-properties</goal></goals>
            <configuration>
                <version>${project.version}</version>
                <userName>${user.name}</userName>
            </configuration>
        </execution>
    </executions>
</plugin>
...

The plugin code, which could be modified to just set all System properties is below:

package com.example.build.maven.mojo;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;

import java.util.Properties;


/**
 * PropertySetterMojo
 *
 * @goal set-properties
 * @phase validate
 * @since 0.1
 */
public class PropertySetterMojo extends AbstractMojo
{
    /**
     * @parameter default-value="${project}"
     * @parameter required
     * @readonly
     */
    private MavenProject project;

    /**
     *  @parameter expression="${session}"
     *  @readonly
     */
    private MavenSession session;

    /**
     * @parameter expression="${mojoExecution}"
     * @readonly
     * @required
     */
    protected MojoExecution execution;


    /**
     *
     */
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException
    {
        try
        {
            Plugin plugin = execution.getPlugin();
            String executionId = execution.getExecutionId();
            PluginExecution pluginExecution = plugin.getExecutionsAsMap().get(executionId);
            Xpp3Dom config = ((Xpp3Dom) pluginExecution.getConfiguration());

            Properties executionProperties = session.getExecutionProperties();

            for (int i = 0; i < config.getChildCount(); i++)
            {
                Xpp3Dom configEntry = config.getChild(i);
                String propertyName = configEntry.getName();
                String propertyValue = configEntry.getValue();

                System.setProperty(propertyName, propertyValue);
                executionProperties.setProperty(propertyName, propertyValue);

                getLog().info("Set System and execution property: " + propertyName + " => " + propertyValue);
            }
        }
        catch (Exception e)
        {
            throw new MojoExecutionException("Failed to set properties", e);
        }
    }
}


In addition to Daniel Stolz answer, here the link to Maven Archetype Specification page: http://maven.apache.org/archetype/maven-archetype-plugin/specification/archetype-metadata.html

An excerpt from the specification, the properties names are highlighted:

[...]

The main properties that are used by the Velocity engine during a project's file generation are groupId, artifactId, version and package.

It is possible to define additional properties that must be valued before the file generation.

[...]


This link has a list of the built-in properties for Maven archetypes - http://intellectualcramps.wordpress.com/2011/04/15/maven-archetype-creation-tips/

The built-in properties are:

  • groupId
  • rootArtifactId - project artifact ID, in single module projects identical with artifactId
  • artifactId - module artifact ID
  • version
  • package – a base java package name, that is placed in src/main/java during project creation.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜