【问题标题】:How to set properties referenced from dependencies in Maven如何在 Maven 中设置从依赖项中引用的属性
【发布时间】:2012-08-23 21:40:45
【问题描述】:

在我的项目中,我有 maven 依赖项。
顺便说一句:

<dependency>
   <groupId>org.mule.modules</groupId>
   <artifactId>mule-module-activiti</artifactId>
   <version>3.2.0</version>
</dependency>

在这个依赖 POM 中有一个使用但未定义的属性 - ${activiti.version}

我发现如何设置此属性的唯一方法是在命令行上指定它,例如mvn -Dactiviti.version=5.10

有什么方法可以在我的项目的 POM 中指定此属性吗?
&lt;properties&gt;&lt;activiti.version&gt;5.10&lt;/activiti.version&gt;&lt;/properties&gt; 不起作用。

编辑:

如果您创建具有依赖关系的新 maven 项目,我试图解决的情况可以重现:

<dependency>
   <groupId>org.mule.modules</groupId>
   <artifactId>mule-module-activiti</artifactId>
   <version>3.2.0</version>
</dependency>

并提供所需的存储库:

  <repositories>
        <repository>
            <id>muleforge-repo</id>
            <name>MuleForge Repository</name>
            <url>http://repository.muleforge.org/release</url>
            <layout>default</layout>
        </repository>

        <repository>
            <id>codehaus-repo</id>
            <name>Codehaus Repository</name>
            <url>http://dist.codehaus.org/mule/dependencies/maven2</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>activiti</id>
            <name>Activiti</name>
            <url>https://maven.alfresco.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>

编辑 2:

这是 org.mule.modules:mule-module-activiti:3.2.0POM,我的项目所依赖的库。在这个 pom 内部,他们使用表达式${activiti.version}。但他们为此表达式设置值。 (没有什么像&lt;properties&gt;&lt;activiti.version&gt;5.10&lt;/activiti.version&gt;&lt;/properties&gt;

问题是如何从 my pom 中为这个表达式设置一个值?

【问题讨论】:

  • 使用 pom 的属性部分是最好的解决方案。为什么它对你不起作用?

标签: maven properties dependency-management


【解决方案1】:

好的,经过几天的搜索和试验,我终于找到了解决问题的方法。

如果需要在命令行中为maven提供属性(说说系统属性,和ma​​ven属性略有不同),也可以使用插件在您的 pom 或单独的属性文件中提供这些属性。

注意: 系统属性可用于覆盖依赖项中的 maven 属性。

像这样:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
        <execution>
            <goals>
                <goal>set-system-properties</goal>
            </goals>
            <configuration>
                <properties>
                    <property>
                        <name>activiti.version</name>
                        <value>5.10</value>
                    </property>
                </properties>
            </configuration>
        </execution>
    </executions>
</plugin>

希望对大家有所帮助。

也为我显然难以理解的笨拙问题感到抱歉。

【讨论】:

  • 非常感谢!我有一个类似的问题,不知道如何解决我的问题,但我很幸运找到了你的帖子!
【解决方案2】:

您可以使用属性来定义版本:

<properties>
  <module-version>3.2.0</module-version>
</properties>
..
<dependency>
   <groupId>org.mule.modules</groupId>
   <artifactId>mule-module-activiti</artifactId>
   <version>${module-version}</version>
</dependency>

为什么不应该that work

您永远不应该通过命令行上的属性定义工件的版本,因为您将来无法重现它,我怀疑它是否有效。

【讨论】:

  • 是的,您所描述的绝对有效。我试图谈论的是当我使用属性定义 IT 依赖项的版本,但没有指定它的值时的情况。检查这个 pom - repository.muleforge.org/release/org/mule/modules/…
  • 集成测试的依赖是否应该与单元测试和发布工件的创建相同。
  • 是的,可能,但我不拥有此依赖项,我需要使用它。我试图避免每次需要构建项目时都必须在命令行上提供参数。
  • 当然不是,但为什么不简单地将版本号提供给你的 pom 呢?此外,您不应在 pom 中定义存储库,而应使用存储库管理器。
  • 我不拥有这个 POM:repository.muleforge.org/release/org/mule/modules/…,我不能改变它,我不能把版本号放在那里。我必须以某种方式提供它。
【解决方案3】:

确保您没有与外界交谈的代理设置。我刚刚尝试了您的配置,并且成功了。我正在使用最新的 Maven 3 版本。

【讨论】:

  • 你设置命令行参数了吗?类似:'mvn clean install -Dactiviti.version=5.10',或者你运行它时不带参数 -'mvn clean install'?
  • 我在 pom.xml 中提供了 3.2.0 等属性。您也可以尝试从命令行执行此操作,它仍然可以工作
  • 我试图详细说明我的问题(编辑 2)。如果我没有指定参数 '-Dactiviti.version=5.10' 我得到:'error: error reading C:\Programs\maven\repository\org\activiti\activiti-engine\${activiti.version}\activiti -engine-${activiti.version}.jar;打开 zip 文件时出错'
猜你喜欢
  • 1970-01-01
  • 2015-07-22
  • 2018-02-25
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多