set java system property during maven 2 compile?

北慕城南 提交于 2019-12-21 17:08:57

问题


I have a maven profile and want to set a property which is later on available per System.getProperty(..) in java:

<profile>
  <id>local-dev</id>
  <properties>
    <my.comp.my.prop>myValue</my.comp.my.prop>
  </properties>
</profile>

I want System.getProperty("my.comp.my.prop") to be "myValue" but it's null.. How do I set it correctly? :)

Thansk!


回答1:


maven cannot set a property which can be accessed by your application from the environment at runtime.

Instead, you can use maven to update a property file in your codebase during build time, which can then be read by your application at runtime. Different values of the property can be set based on the profile, thereby allowing your application to have different values as desired.

Alternately, you can invoke the application setting the desired property in the environment manually (outside maven).




回答2:


properties-maven-plugin plugin will help you to do exactly what you're looking for:

<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>my.property.name</name>
                        <value>my.property.value</value>
                    </property>
                </properties>
            </configuration>
        </execution>
    </executions>
</plugin>


来源:https://stackoverflow.com/questions/9707219/set-java-system-property-during-maven-2-compile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!