java - 我在哪里可以存储此属性以供 Maven 和 Java 使用?

标签 java maven properties

我正在使用 Maven 3.0.3 和 Java 6。我想将 WSDL URL 属性放在 Maven 构建过程可以访问和我的运行时 Java 代码(我正在构建 Maven JAR 项目)也可以访问的地方.我将如何构建/配置它?在我的运行时 Java 代码中,我有类似的东西

String wsdlUrl = getProperty("wsdl.url");

在 Maven 中,我想像这样访问插件中的 WSDL URL ...

                    <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>jaxws-maven-plugin</artifactId>
                            <executions>
                                    <execution>
                                            <goals>
                                                    <goal>wsimport</goal>
                                            </goals>
                                            <configuration>
                                                    <wsdlUrls>
                                                            <wsdlUrl>${wsdl.url}</wsdlUrl>
                                                    </wsdlUrls>
                                                    <sourceDestDir>${basedir}/src/main/java</sourceDestDir>
                                                    <packageName>org.myco.bsorg</packageName>
                                            </configuration>
                                    </execution>
                            </executions>
                    </plugin>

最佳答案

src/main/resources 目录中创建一个 .properties 文件。

在 pom.xml 中

使用 properties-maven-plugin并从此文件加载属性,如下所示(在 usage site 之后):

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>src/main/resources/common.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

来自 Java

使用Properties#load(InputStream)并像这样加载属性:

String propertiesFileName = "/common.properties";

Properties properties = new Properties();
InputStream inputStream = this.getClass().getClassLoader()
    .getResourceAsStream(propertiesFileName);

properties.load(inputStream);

关于java - 我在哪里可以存储此属性以供 Maven 和 Java 使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10885077/

相关文章:

java - 获取和设置键值属性

java - Java中HashMap和ArrayList的区别?

java - 非均匀随机发生器

maven - 如何在执行 maven-resources-plugin :copy-resources 时在 Maven 复制资源过程中失败,而不是跳过不存在的资源目录

java - maven构建模块中的Jahia6.6部署错误

java - 为 ESAPI 设置资源目录

java - 如何将复杂的map嵌套列表转换为json对象

java - 需要有关创建 Node 类 (java) 的指导?

java - 具有 https 连接的 Maven WebService

java - 无法从 java 属性文件中删除 key