How to read an external properties file in Maven
Try the
Properties Maven Plugin
How to read properties file from external jar in maven project
There are numerous ways to do this.
Assuming your xyz.properties
file looks like this:
a=b
y=z
mykey=myvalue
Using java.util.ResourceBundle
If you don't need to pass around the read data as a Properties
object, this is the easiest way.
ResourceBundle rb = ResourceBundle.getBundle("xyz");
System.out.println("a=" + rb.getString("a"));
Using java.util.Properties
A bit more work and some boiler-plate, but accomplishes the same thing.
Properties p = new Properties();
try (InputStream is = getClass().getClassLoader().getResourceAsStream("xyz.properties")) {
p.load(is);
}
catch (IOException e) {
// Handle as appropriately.
}
System.out.println("mykey=" + p.getProperty("mykey"));
Maven - Reading a property from an external properties file
The initialize
phase is not part of the clean lifecycle. You need to also bind your properties plugin to pre-clean
phase.
However, the dependency resolution runs before resolving and executing other plugins, so your approach won't work.
The proper way to deal with that would be to move dependency versions into a parent pom.xml and use the same parent pom in both of your projects.
How to load an external properties file from a maven java project
I hope you are using maven jar plugin. If so use this configuration inside jar-plugin
<configuration>
<archive>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
Then the jar will take the properties file from root directory of jar file.
Specify pom properties via a properties file?
in the pom you can place...
<properties>
<core-version>1234</core-version>
<lib-version>1234</lib-version>
<build-version>9999</lib-version>
<build-date>20150101</build-date>
</properties>
with all the properties you require.
Or you can use...
<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>dev.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
and the file dev.properties
will contain the properties
core-version=1234
lib-version=1234
build-version=9999
build-date=20150101
...
Or... you can inject the properties using a settings.xml file as shown here
You may also find the Maven build number plugin useful... here
External properties file in spring
Here is what you can do:
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource(value = "file:/etc/config/my-custom-config.properties", ignoreResourceNotFound = true)
})
This ignoreResourceNotFound
is available since spring 4.3 and is pretty self-explanatory.
You can also opt for "programmatic" approach. In pure spring (not spring boot as you've mentioned in the question):
@Configuration
public class CommonConfig {
...
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
ppc.setLocations(new FileSystemResource("/etc/config/my-custom-config.properties"),
new ClassPathResource("config/application.properties"),
return ppc;
}
...
}
FileSystemResource
is for accessing resources available externally in the file system
ClassPathResource
is for accessing resources in the classpath
How to load external properties file with Java without rebuilding the Jar?
You can try this one, which will first try to load properties file from project home directory so that you don't have to rebuild jar, if not found then will load from classpath
public class Hello {
public static void main(String[] args) {
String configPath = "test.properties";
if (args.length > 0) {
configPath = args[0];
} else if (System.getenv("CONFIG_TEST") != null) {
configPath = System.getenv("CONFIG_TEST");
}
File file = new File(configPath);
try (InputStream input = file.exists() ? new FileInputStream(file) : Hello.class.getClassLoader().getResourceAsStream(configPath)) {
Properties configProperties = new Properties();
configProperties.load(input);
System.out.println(configProperties.getProperty("first") + " " + configProperties.getProperty("last"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
You can send the properties file path as argument or set the path to an environment variable name CONFIG_TEST
pom.xml is not able to read properties from external property file
To my knowledge, it is not possible to do what you're attempting to do (i.e. externalizing the version of an artifact). This is simply because Maven needs the whole dependency set before any plugin runs / before you're reading stuff from your file (it's a chicken-and-egg situation).
Related Topics
How to Exclude Some Concrete Urls from <Url-Pattern> Inside <Filter-Mapping>
How to Load Rsa Private Key from File
How to Interrupt a Serversocket Accept() Method
Should I Keep My Project Files Under Version Control
Big-O Summary for Java Collections Framework Implementations
How to Log SQL Statements in Spring Boot
How to Convert Object Array to String Array in Java
Where Does Java's String Constant Pool Live, the Heap or the Stack
How to Decrypt a Sha-256 Encrypted String
Load Chrome Profile Using Selenium Webdriver Using Java
Is Java.Sql.Connection Thread Safe
How to Generate a Random Biginteger Value in Java
What's the Syntax to Import a Class in a Default Package in Java
How to Write Data with Fileoutputstream Without Losing Old Data