Java Maven - 我可以在配置文件定义中引用配置文件 ID 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2012198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:30:45  来源:igfitidea点击:

Maven - Can I reference profile id in profile definition?

javamaven-2maven

提问by Ula Krukar

I've set profiles in a pom.xml, like shown as follows:

我已经在 a 中设置了配置文件,pom.xml如下所示:

<profile>
<id><em>profileId1</em></id>
    <build>
        <filters>
            <filter>src/main/filters/<em>profileId1</em>.properties</filter>
        </filters>
// rest of the profile 
</profile>
<profile>
<id><em>profileId2</em></id>
    <build>
        <filters>
            <filter>src/main/filters/<em>profileId2</em>.properties</filter>
        </filters>
// rest of the profile
</profile>

Question:

题:

Is there any way to extract this piece from all the profiles, so that there is no need to repeat it for every profile (and possibly misspell it)?

有没有办法从所有配置文件中提取这一段,这样就不需要为每个配置文件重复它(并且可能拼错了)?

回答by Pascal Thivent

According to PLXUTILS-37, it should be possible to access properties in a List or Map using "Reflection Properties" (see the MavenPropertiesGuidefor more about this).

根据PLXUTILS-37,应该可以使用“反射属性”访问 List 或 Map 中的属性(有关更多信息,请参阅MavenPropertiesGuide)。

So just try ${project.profiles[0].id}, ${project.profiles[1].id}, etc.

所以就试试${project.profiles[0].id}, ${project.profiles[1].id}, 等等。

If this doesn't work (I didn't check if it does), I'd use profile activation based on a system property as described in Introduction to build profilesand use that property in the filter. Something like that:

如果这不起作用(我没有检查它是否起作用),我将使用基于系统属性的配置文件激活,如构建配置文件简介中所述,并在过滤器中使用该属性。类似的东西:

<profile>  
  <id>profile-profileId1</id>  
  <activation>
    <property>
      <name>profile</name>
      <value>profileId1</value>
    </property>
  </activation>
  <build>  
    <filters>  
      <filter>src/main/filters/${profile}.properties</filter>  
    </filters>  
    // rest of the profile  
</profile>

To activate this profile, you would type this on the command line:

要激活此配置文件,您可以在命令行中输入:

mvn groupId:artifactId:goal -Dprofile=profileId1 

回答by alfonx

With maven 2.2.1 and later, I was able to get the ID of the first active profile using:

使用 maven 2.2.1 及更高版本,我能够使用以下方法获取第一个活动配置文件的 ID:

${project.activeProfiles[0].id}

Of course this fails if there is not a least one active profile.

当然,如果没有至少一个活动配置文件,这将失败。

Using the

使用

${project.profiles[0].id}

as suggested by Pascal did notwork for me.

帕斯卡的建议并没有为我工作。

Hint: While investigating this, I really started to love mvn help:evaluate.

提示:在调查这个时,我真的开始喜欢mvn help:evaluate.

回答by Mike R

As an alternative to ${project.activeProfiles[0].id}(which doesn't seem to work on older versions of maven), just define a property:

作为替代${project.activeProfiles[0].id}(它似乎不适用于旧版本的 maven),只需定义一个属性:

    <profile>
        <id>dev</id>
        <properties>
            <profile-id>dev</profile-id>
        </properties>
    </profile>

Then use ${profile-id}.

然后使用${profile-id}.

Note: just make sure one is always active by default

注意:只需确保默认情况下始终处于活动状态