42

重複を減らすために、コンパイル時に Java クラスで Maven プレースホルダーを使用したいだけです。

そんな感じ:

pom.xml

<properties>
  <some.version>1.0</some.version>
</properties>

SomeVersion.java

package some.company;

public class SomeVersion {

    public static String getVersion() {
        return "${some.version}"
    }

}
4

4 に答える 4

65

simply create file app.properties in src/main/resources with content like this

application.version=${project.version}

then enable maven filtering like this

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

That's all - in app code just read properties file

ClassPathResource resource = new ClassPathResource( "app.properties" );
p = new Properties();
InputStream inputStream = null;
try {
    inputStream = resource.getInputStream();
    p.load( inputStream );
} catch ( IOException e ) {
    LOGGER.error( e.getMessage(), e );
} finally {
    Closeables.closeQuietly( inputStream );
}

and provide method like this

public static String projectVersion() {
    return p.getProperty( "application.version" );
}
于 2012-07-31T13:03:40.393 に答える
10

Even though it's not a very nice solution it is possible with the default maven resource plugin.

First you need to specify the resource plugin.

<project>
  <build>
    <!-- Configure the source files as resources to be filtered
      into a custom target directory -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <filtering>true</filtering>
        <targetPath>../filtered-sources/java</targetPath>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
</project>

Afterwards you will need to change the 'default' configuration of the compiler plugin.

<project>
  <build>
      <!-- Overrule the default pom source directory to match
            our generated sources so the compiler will pick them up -->
      <sourceDirectory>target/filtered-sources/java</sourceDirectory>
  </build>
</project> 
于 2012-07-31T13:03:42.390 に答える
2

Spring を使用している場合は、プロパティを注入できます。手順は次のとおりです。

  1. POM ファイル内で、必要なすべてのプロファイルを定義し、各プロファイルにはカスタム プロパティが必要です。

<profile>
	<id>dev</id>
	<properties>
		<some.version>Dev Value</some.version>
	</properties>
</profile>

  1. プロファイルのセクション ビルドで、フィルタリング インジェクションを定義します。
  2. プロジェクトのリソース ディレクトリの下に、プロパティ ファイル (覚えやすいキリスト教の名前) を作成し、挿入するプロップを配置します。

custom.some.version=${some.version}

  1. spring-context ファイルで、プロパティ プレースホルダーを定義し、Bean または beanProperty を定義します。

<context:property-placeholder location="classpath*:/META-INF/*.properties"/>
...
<bean id="customConfig" class="com.brand.CustomConfig">
	<property name="someVersion" value="${custom.some.version}" />
</bean>
...

  1. クラスを作成します。
package com.brand;

public class CustomConfig {
  private String someVersion;

  public getSomeVersion() {
  return this.someVersion;
  }

  public setSomeVersion(String someVersion) {
  this.someVersion = someVersion;
  }
}
  1. 使いたいところに注入。この例は autowired Bean を使用していますが、autowired プロパティも使用できます。
package com.brand.sub

public class YourLogicClass {
  @Autowired
  private CustomConfig customConfig;

  // ... your code
}

最終的なコンパイルでは、正しい値が得られます。

于 2015-11-04T20:05:12.867 に答える