4

我正在使用 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>
4

3 回答 3

5

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

pom.xml 里面

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

<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);
于 2012-06-04T19:21:47.223 回答
2

您应该使用Maven 资源过滤来完成此操作。

这是一个适合您的示例。

layout

+- pom.xml
+- src
  +- main
    +- java
      +- resources
        +- application.properties

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company</groupId>
    <artifactId>application</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <wsdl.url>http://some.url</wsdl.url>
    </properties>

    <build>
        <plugins>
            <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>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

src/main/resources/application.properties

wsdl.url = ${wsdl.url}

然后在您的代码中,您可以加载属性文件并获取属性wsdl.url

src/main/java/com/company/Main.java

package com.company;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author maba, 2012-06-04
 */
public class Main {

    public static void main(String[] args) throws IOException {
        ClassLoader loader = Main.class.getClassLoader();
        InputStream in = loader.getResourceAsStream("application.properties");
        Properties properties = new Properties();
        properties.load(in);
        String url = (String) properties.get("wsdl.url");
        System.out.println(url);
    }
}
于 2012-06-04T19:17:39.427 回答
0

src/main/resources 或 src/test/resources 可被 maven 访问,并且会在构建时自动按摩到 jar 中。

于 2012-06-04T16:59:12.270 回答