代码之家  ›  专栏  ›  技术社区  ›  OscarRyz

为Maven项目指定系统属性

  •  56
  • OscarRyz  · 技术社区  · 16 年前

    有没有一种方法(我的意思是如何在Maven项目中设置系统属性)?

    我想从我的测试和我的WebApp(本地运行)中访问一个属性,我知道我可以使用Java系统属性。

    我应该把它放在./settings.xml或类似的地方吗?

    语境

    我接受了一个开源项目,并设法将数据库配置更改为使用JavaDB。

    现在,在JavaDB的JDBC URL中,可以将数据库的位置指定为完整路径(请参见: this other question )

    或系统属性: derby.system.home

    我已经让代码工作了,但目前所有代码都硬编码到:

     jdbc:derby:/Users/oscarreyes/javadbs/mydb
    

    我想删除完整的路径,就像:

     jdbc:derby:mydb
    

    为此,我需要指定系统属性( 德比.系统.主页 )给马文,但我不知道怎么做。

    测试是使用JUnit(我在pom.xml中没有看到任何插件)执行的,Web应用程序使用jetty插件运行。

    在命令行中指定系统属性似乎对Jetty有效,但我不确定这是否可行(假定其他一些用户可以从eclipse/idea/whatever运行它)。

    4 回复  |  直到 9 年前
        1
  •  65
  •   Pascal Thivent    16 年前

    有没有一种方法(我的意思是如何在Maven项目中设置系统属性)?我想从测试中访问属性[…]

    您可以在 Maven Surefire Plugin 配置(这是有意义的,因为测试在默认情况下是分叉的)。从 Using System Properties :

    <project>
      [...]
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.5</version>
            <configuration>
              <systemPropertyVariables>
                <propertyName>propertyValue</propertyName>
                <buildDirectory>${project.build.directory}</buildDirectory>
                [...]
              </systemPropertyVariables>
            </configuration>
          </plugin>
        </plugins>
      </build>
      [...]
    </project>
    

    和我的webapp(本地运行)

    不知道这里的意思,但我假设webapp容器是由maven启动的。可以使用以下命令行传递系统属性:

    mvn -DargLine="-DpropertyName=propertyValue"
    

    更新: 好的,现在明白了。对于Jetty,您还应该能够在Maven Jetty插件配置中设置系统属性。从 Setting System Properties :

    <project>
      ...
      <plugins>
        ...
          <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <configuration>
             ...
             <systemProperties>
                <systemProperty>
                  <name>propertyName</name>
                  <value>propertyValue</value>
                </systemProperty>
                ...
             </systemProperties>
            </configuration>
          </plugin>
      </plugins>
    </project>
    
        2
  •  57
  •   Markus    10 年前

    properties-maven-plugin 插件可能有帮助:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0.0</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>
    
        3
  •  9
  •   bvesco    12 年前

    我已经知道,如果你正在做一个“独立”的Java应用程序,也可以用Excel Maven插件来实现这一点。

                <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>${maven.exec.plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>${exec.main-class}</mainClass>
                    <systemProperties>
                        <systemProperty>
                            <key>myproperty</key>
                            <value>myvalue</value>
                        </systemProperty>
                    </systemProperties>
                </configuration>
            </plugin>
    
        4
  •  2
  •   David Lotts    9 年前

    如果测试和webapp在同一个maven项目中,则可以在项目pom中使用属性。然后您可以过滤某些文件,这将允许Maven在这些文件中设置属性。有不同的过滤方法,但最常见的是在资源阶段- http://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-description.html

    如果测试和webapp位于不同的maven项目中,则可以将属性放在settings.xml中,该文件位于Windows上的maven存储库文件夹(c:\documents and settings\username.m2)中。您仍然需要使用筛选或其他方法将属性读取到测试和webapp中。

    推荐文章