【问题标题】:Are there other default maven archetype properties是否有其他默认的 Maven 原型属性
【发布时间】:2011-06-30 16:14:54
【问题描述】:

我正在创建一个具有大量自定义属性的 maven 原型。

例如:

<requiredProperties>
  <requiredProperty key="db-name">
    <defaultValue>Some db-name</defaultValue>
  </requiredProperty>
  <requiredProperty key="station-name">
    <defaultValue>localhost</defaultValue>
  </requiredProperty>
  ...
</requiredProperties>

当基于此原型生成新项目时,maven 知道一些默认变量,例如 groupId、artifactId、version。 maven 是否知道其他琐碎的变量,例如 env.user、user、host、path、basedir 或任何其他变量? 哪个是? 我怎样才能得到它们?

谢谢。

【问题讨论】:

  • 嗨,米歇尔,请说明您想将变量用于什么用途。原型使您可以从模板(原型)创建类似的项目,但如果您需要使用 f.e.系统属性它们不必在原型的项目生成中设置。 Maven 知道坐标,因为您在生成过程中输入了它们。

标签: maven maven-archetype


【解决方案1】:

我意识到这是一个老问题,但我早些时候遇到了这个问题并投了赞成票。我也想知道,我现在想出了一个解决方案/解决方法来启用对这些属性的访问。

我创建了自己的 Maven 插件,命名为 property-setter-maven-plugin,它设置了 System 和最重要的 execution 属性。该插件的配置允许指定任意数量的属性和值(并且由于它们是在 POM 中定义的,因此可以访问所有普通变量)。然后运行原型插件时(在与我的自定义插件相同的 Maven 执行中),它​​会读取执行属性并找到您配置的任何内容。

我的 Maven 命令如下所示:

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

位于生成原型的同一目录中的 POM 中的配置如下所示:

...
<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>
...

插件代码,可以修改为只设置所有系统属性如下:

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);
        }
    }
}

【讨论】:

    【解决方案2】:

    除了 Daniel Stolz 的回答,这里是 Maven Archetype Specification 页面的链接: http://maven.apache.org/archetype/maven-archetype-plugin/specification/archetype-metadata.html

    规范的摘录,属性名称被突出显示:

    [...]

    Velocity 引擎在项目文件生成期间使用的主要属性是groupId、artifactId、version 和package

    可以定义在文件生成之前必须赋值的附加属性。

    [...]

    【讨论】:

      【解决方案3】:

      此链接包含 Maven 原型的内置属性列表 - http://intellectualcramps.wordpress.com/2011/04/15/maven-archetype-creation-tips/

      内置属性有:

      • groupId
      • rootArtifactId - 项目工件 ID,在与 artifactId 相同的单个模块项目中
      • artifactId - 模块工件 ID
      • 版本
      • package – 一个基本的 java 包名,在项目创建期间放置在 src/main/java 中。

      【讨论】:

      • 将相关列表直接插入答案会比提供链接(可能会在一段时间内失效)更好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      • 2018-04-10
      • 1970-01-01
      • 2012-01-30
      相关资源
      最近更新 更多