Dave A
Dave A

Reputation: 2830

Where can I store this property for use by Maven and Java?

I'm using Maven 3.0.3 and Java 6. I want to put a WSDL URL property somewhere that both the Maven build process can acccess and my runtime Java code (I'm building a Maven JAR project) can also access. How would I structure/configure this? In my runtime Java code, I have something like

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

and in Maven, I want to access the WSDL URL in a plugin like so ...

                    <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>

Upvotes: 4

Views: 4835

Answers (3)

maba
maba

Reputation: 48045

You should be using maven resource filtering to accomplish that.

Here is an example that will work for you.

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}

Then in your code you can load the properties file and get the property 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);
    }
}

Upvotes: 2

npe
npe

Reputation: 15699

Create a .properties file inside the src/main/resources directory.

Inside pom.xml

Use a properties-maven-plugin and load properties from this file, like this (after the 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>

From Java

Use Properties#load(InputStream) and load properties like this:

String propertiesFileName = "/common.properties";

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

properties.load(inputStream);

Upvotes: 5

Mike Thomsen
Mike Thomsen

Reputation: 37506

src/main/resources or src/test/resources is accessible to maven and will be automatically massaged into the jar at build time.

Upvotes: 0

Related Questions