Czy istnieje sposób użycia właściwości Maven w klasie Java podczas kompilacji

Chcę tylko użyć maven placeholder w mojej klasie Javy podczas kompilacji, aby zmniejszyć powielanie.

Coś w tym stylu:

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

}
Author: Donald Duck, 2012-07-31

4 answers

Po prostu utwórz aplikację plików.właściwości w src / main / resources z zawartością taką jak Ta

application.version=${project.version}

Następnie Włącz filtrowanie Mavena w ten sposób

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

To wszystko w kodzie aplikacji wystarczy odczytać plik właściwości

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 );
}

I podaj taką metodę

public static String projectVersion() {
    return p.getProperty( "application.version" );
}
 53
Author: Andrey Borisov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-03 14:20:38

Mimo, że nie jest to bardzo ładne rozwiązanie jest możliwe z domyślną wtyczką zasobów maven.

Najpierw musisz określić wtyczkę zasobów.

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

Następnie będziesz musiał zmienić 'domyślną' konfigurację wtyczki kompilatora.

<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> 
 10
Author: Jeroen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-31 13:03:42

Najprostszym sposobem, jaki znam, jest użycie template Maven Plugin.

Dodaj deklarację wtyczki do pom:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>templating-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <id>filter-src</id>
            <goals>
                <goal>filter-sources</goal><!-- add this if you filter main sources -->
                <goal>filter-test-sources</goal><!-- add this if you filter test sources -->
            </goals>
        </execution>
    </executions>
</plugin>

Jeśli filtrujesz główne źródła:

  • Utwórz folder src/main/java-templates
  • Przenieś pliki źródłowe, które chcesz filtrować do tego folderu. Reprodukować strukturę drzewa pakietów, tak jakbyś był w src/main.

Jeśli filtrujesz też źródła testów:

  • Utwórz folder src/test/java-templates
  • Przenieś pliki źródłowe chcesz być przefiltrowany do tego folderu. Reprodukować strukturę drzewa pakietów, tak jakbyś był w src/test.

Zakładając, że Twoje źródła zawierają ważne symbole zastępcze, takie jak:

package some.company;

public class SomeVersion {

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

}

Teraz, gdy compile lub test twój projekt, te elementy zastępcze powinny być już wycenione.

Mam nadzieję, że to pomoże.
 2
Author: aymens,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-21 09:34:49

Jeśli pracujesz z Springiem, możesz wstrzyknąć nieruchomość. Kroki są:

  1. wewnątrz pliku POM definiujesz wszystkie potrzebne profile i każdy profil musi mieć własną właściwość, w Twoim przypadku

<profile>
	<id>dev</id>
	<properties>
		<some.version>Dev Value</some.version>
	</properties>
</profile>
  1. w sekcji Budowa Twojego profilu definiujesz Wtrysk filtrowania.
  2. w katalogu your project resources tworzysz plik properties (dowolna nazwa mnemotechniczna) i umieszczasz swój Propp do zastrzyku:

Zwyczaj.trochę.version=${some.wersja}

  1. w pliku spring-context definiujesz properties placeholder i definiujesz bean lub 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. Stwórz swoją klasę.
package com.brand;

public class CustomConfig {
  private String someVersion;

  public getSomeVersion() {
  return this.someVersion;
  }

  public setSomeVersion(String someVersion) {
  this.someVersion = someVersion;
  }
}
  1. wstrzyknąć tam, gdzie chcesz. Ten przykład dotyczy autowired bean, ale możesz również użyć właściwości i autowired.
package com.brand.sub

public class YourLogicClass {
  @Autowired
  private CustomConfig customConfig;

  // ... your code
}

Na ostatecznej kompilacji, masz prawidłowe wartości.

 1
Author: ELD,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-04 20:17:21