Access maven property using dependency management
You can only inherit properties from another pom, if you declare that as a parent. Importing a pom with type import
only imports its dependencies, as described in the documentation. Since using the other pom as a parent is not possible in your case, let me suggest an alternative:
The codehaus Properties Maven Plugin can load maven properties from an external file. It can even use classpath:
URLs to load files from. So you might try to load those from another dependency (which should have an appropriate scope
since you probably do not want that dependency's JAR to hang around at runtime).
Is there a way to use maven property in Java class during compilation
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" );
}
how to access properties defined in other module pom in maven multimodule project
Explanation provided in answer of this question explains it in a very good manner that what can be used and when.
Reading Properties file from POM file in Maven
What does properties tag mean in pom.xml maven?
What does properties tag mean ?
From Official Maven Docs :
Properties are the last required piece to understand POM basics. Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example:
In your case you have defined properties as version of java.
Now this property(java.version
) can be reused later in maven pom file.
From Official Maven Docs :
They come in five different styles:
env.X: Prefixing a variable with "env." will return the shell's environment variable. For example, ${env.PATH} contains the PATH
environment variable. Note: While environment variables themselves are
case-insensitive on Windows, lookup of properties is case-sensitive.
In other words, while the Windows shell returns the same value for
%PATH% and %Path%, Maven distinguishes between ${env.PATH} and
${env.Path}. The names of environment variables are normalized to all
upper-case for the sake of reliability.project.x: A dot (.) notated path in the POM will contain the corresponding element's value. For example:
1.0 is accessible via
${project.version}.settings.x: A dot (.) notated path in the settings.xml will contain the corresponding element's value. For example:
false is accessible via
${settings.offline}.Java System Properties: All properties accessible via java.lang.System.getProperties() a-re available as POM properties,
such as ${java.home}.x: Set within a element in the POM. The value of value may be used as
${someVar}.
What can i add also in properties tag ?
You can add all the variables which you need to reuse later in your maven pom file.
For e.g. Below POM snippet reuses jackson.version 4 times.
<properties>
<jackson.version>2.10.2</jackson.version>
<dropwizard.version>2.0.1</dropwizard.version>
<websocket.version>1.4.0</websocket.version>
<apachehttp.version>4.5.10</apachehttp.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${apachehttp.version}</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>${websocket.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependencies>
References :
Maven Pom Properties
Can't access Maven properties with Spring
You specified properties in profile, so there is a workaround. Annotations such as @Value("${name}")
are referencing to externalized configuration, e.g. from application.yml. You should use this file to load properties you want. @property_name@
loads property from your maven profile.
jdbc:
url: @jdbc.url@
username: @jdbc.username@
and so on...
Then these properties will be accessible in your code.
Related Topics
Java: Calling a Super Method Which Calls an Overridden Method
Installing Java on Os X 10.9 (Mavericks)
Annotation to Make a Private Method Public Only for Test Classes
How to Get Java Logging Output to Appear on a Single Line
Which Loop Has Better Performance? Why
Retrieve an Image Stored as Blob on a MySQL Db
Compile Error: Package Javax.Servlet Does Not Exist
How to Loop Over a Class Attributes in Java
Found Shared References to a Collection Org.Hibernate.Hibernateexception
Simplest Way to Print an 'Intstream' as a 'String'
Lists with Wildcards Cause Generic Voodoo Error
Java:Does Wait() Release Lock from Synchronized Block
How to Find the Source Code for Java's Square Root Function
What Is the Benefit of Polymorphism Using Collection Interface to Create Arraylist Object
How to Deserialize a Blank JSON String Value to Null for Java.Lang.String