Maven - 如何从 Eclipse 引用环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15632804/
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-09-19 20:15:52  来源:igfitidea点击:

Maven - How to refer environment variable from eclipse

eclipsemaven

提问by deepujain

I am have a maven project. In pom.xmli refer a environment variable as ${env.MyProjectVersion}. I set the value (export MyProjectVersion=1.0.0) of the variable from command prompt before building the project. The build is successful, I import the maven project into eclipseand m2eclipseattempts to build it failing:

我有一个 Maven 项目。在pom.xml我将环境变量称为${env.MyProjectVersion}. export MyProjectVersion=1.0.0在构建项目之前,我从命令提示符设置了变量的值 ( )。构建成功,我将 maven 项目导入到eclipse 中m2eclipse尝试构建它失败:

Unable to download xyz-{$env.MyProjectVersion} dependency

无法下载 xyz-{$env.MyProjectVersion} 依赖项

Eclipseis not able to resolve the value of environment variable. Is there a way to specify environment variable value with eclipse so that it can be picked by maven plugin in eclipse.

Eclipse无法解析环境变量的值。有没有办法用eclipse指定环境变量值,以便eclipse中的maven插件可以选择它。

回答by systemkern

Apparently the Eclipse Maven plugin ignores environment variables during maven builds. A possible workaround - which might work depending on your situation - is to use properties from the settings.xml. The only drawback is that you have to define those variables as environmental variables and a second time in the settings.xml

显然 Eclipse Maven 插件在 Maven 构建过程中忽略了环境变量。一种可能的解决方法(根据您的情况可能有效)是使用 settings.xml 中的属性。唯一的缺点是您必须将这些变量定义为环境变量,并在 settings.xml 中再次定义

In my answer I am describing the following scenario. Building artefacts in eclipse using the standard maven plugin functionality and additionally building the same artifacts via the maven command line. Also during command line builds i am referencing the repository folder directly mvn -Dmaven.repo.local=/PATH_TO_M2_FOLDER/repository/ -f parent.pom install

在我的回答中,我描述了以下场景。使用标准的 maven 插件功能在 eclipse 中构建工件,并通过 maven 命令行额外构建相同的工件。同样在命令行构建期间,我直接引用存储库文件夹 mvn -Dmaven.repo.local=/PATH_TO_M2_FOLDER/repository/ -f parent.pom install

The setup is as follows: You will have to add properties in the pom.xml to act as proxies for your environment variables. In the command line scenario these properties will be filled directly. In the Eclipse scenario the properties have to be filled by the user settings.xml by defining a profile.

设置如下: 您必须在 pom.xml 中添加属性以充当环境变量的代理。在命令行场景中,这些属性将直接填充。在 Eclipse 场景中,属性必须由用户 settings.xml 通过定义配置文件来填充。

Say you have a property an environment variable $FOO.

假设您有一个环境变量 $FOO 的属性。

Use a profile in your maven settings.xml and define fooProperty.

在您的 maven settings.xml 中使用配置文件并定义 fooProperty。

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
    http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <profiles>
        <profile>
            <id>default</id>
            <properties>
                <fooProperty>bar</fooProperty >
            </properties>
        </profile>
    </profiles>
    ...     
  <activeProfiles>
    <activeProfile>default</activeProfile>
  </activeProfiles>
</settings>

Then you just have to point the Eclipse maven plugin to your user settings. For CLI to work you have to define the fooProperty a second time in your pom.xml: ${env.foo}

然后您只需将 Eclipse maven 插件指向您的用户设置。要使 CLI 工作,您必须在 pom.xml 中再次定义 fooProperty:${env.foo}

Apparently the eclipse maven plugin prioritises properties from the settings.xml higher than properties in the pom.xml. Unfortunately I have not figured out a way of defining the property in the settings.xml via an environment variable.

显然,eclipse maven 插件将 settings.xml 中的属性优先于 pom.xml 中的属性。不幸的是,我还没有想出一种通过环境变量在 settings.xml 中定义属性的方法。

When using the variable in the pom.xml only use the proxy property as follows: ... ${fooProperty} ...

当使用 pom.xml 中的变量时,只使用代理属性如下: ... ${fooProperty} ...

I am using * Eclipse Mars: Release (4.5.0) * Embedded Maven: 3.3.3/1.6.0.20150526-2031 * OSX El Capitan: 10.11.4 (15E65)

我正在使用 * Eclipse Mars:发布 (4.5.0) * 嵌入式 Maven:3.3.3/1.6.0.20150526-2031 * OSX El Capitan:10.11.4 (15E65)

回答by zaffargachal

Go to Run Configuration, Goto Enviroment tab, add variable and its value than value of variable will be resolved

转到运行配置,转到环境选项卡,添加变量,其值将被解析为变量值