Little stuck here. I have a pom with 3 profiles. Theese profiles have different version name. I want to inject that version name into properties file when a specific profile is building.
My profiles:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<projectVersion>DEV</projectVersion>
</properties>
</profile>
<profile>
<id>test</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<projectVersion>1.0.0-RC1</projectVersion>
</properties>
</profile>
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<projectVersion>1.0.0-Final</projectVersion>
</properties>
</profile>
</profiles>
and filter.properties looks like this:
projectName = defaultName
versionName = defaultVersion
How to do that? Im building project by command:
mvn clean install -D profile_name
What you need to do is to add a new section to your <build> section of your POM file.
Like this:
<build>
<resources>
<resource>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
This will look inside the specified folder (src/main/resources) on the specified files **/*.properties and change the files when it encounters defined variables.
So in order to this work your propertie file must be this way:
projectName = ${defaultName}
versionName = ${defaultVersion}
Be aware with these variables name. Maven will replace it with the defined names by you or the names of the Maven structure like ${projectVersion} will be replaced by the <version>1.0</version> tag of your pom file.
So instead of using:
<properties>
<projectVersion>1.0.0-Final</projectVersion>
</properties>
Change the name (and the version) of this variable to something else like:
<properties>
<defaultVersion>1.0.0-Final</defaultVersion>
<defaultName>someName</defaultName>
</properties>
On all your profiles.
And just run your maven command as:
mvn install -Pprofilename
Be careful with the profiles you shown. All of them are active by default and this is a problem because they all define the same maven property. Instead, you should mark only one as active by default.
You also don't show <resources> filtering to process filter.properties, so this can be a mistake, as well.
And a final though, you are controlling artifact version on maven profiles. I don't think it is a good idea. Please read about maven-release-plugin.
Related
My current project profiles in pom.xml are following this.
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>production</id>
</profile>
<profile>
<id>development</id>
</profile>
</profiles>
I am looking for a way to print to check if this build is on local in .java code.
I also would like to make some functions divided.
System.out.println(isLocal)
/* When it in local. it should be true*/
if(isLocal)
{
XXX
}
else
{
YYY
}
Project is compiled like "mvn compile -P local"
in Eclipse
You probably have to do the following:
Define a property inside the profile.
Define a properties file in your resources.
Use resource filtering to write the property to this properties file.
Load the resource in your Java code and check the property within.
I have a Maven pom.xml with a plugin that I want to be able to control on the command line. Everything works otherwise fine, except even after searching the net a while I can't figure out how to set a default value for my control property:
<plugin>
...
<configuration>
<param>${myProperty}</param>
</configuration>
...
</plugin>
So if I run Maven with
mvn -DmyProperty=something ...
everything's fine, but I'd like to have a specific value assigned to myProperty also without the -DmyProperty=... switch. How can this be done?
You can have the property default value defined in <build>/<properties> or in a profile like shown below. When you supply the property value on command line with -DmyProperty=anotherValue then it will override the definition from the POM. That is, all definitions of property values in the POM are set only a default value for the properties.
<profile>
...
<properties>
<myProperty>defaultValue</myProperty>
</properties>
...
<configuration>
<param>${myProperty}</param>
</configuration>
...
</profile>
Taylor L's approach works fine, but you don't need the extra profile. You can just declare property values in the POM file.
<project>
...
<properties>
<!-- Sets the location that Apache Cargo will use to install containers when they are downloaded.
Executions of the plug-in should append the container name and version to this path.
E.g. apache-tomcat-5.5.20 -->
<cargo.container.install.dir>${user.home}/.m2/cargo/containers</cargo.container.install.dir>
</properties>
</project>
You can also set properties in your user settings.xml file in the event that you want each user to be able to set their own defaults. We use this approach to hide credentials that the CI server uses for some plug-ins from regular developers.
You could use something like below:
<profile>
<id>default</id>
<properties>
<env>default</env>
<myProperty>someValue</myProperty>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
#akostadinov's solution works great for common usage... But if the desired property shall be used by reactor component during dependency resolution phase (very early in mvn pom hierarchy processing...) you should use profile "none activation" test mechanism to ensure the optional command line provided value is always prioritized regarding the value provided inside pom.xml. And this whatever deep is your pom hierarchy.
To do so, add this kind of profile in your parent pom.xml :
<profiles>
<profile>
<id>my.property</id>
<activation>
<property>
<name>!my.property</name>
</property>
</activation>
<properties>
<my.property>${an.other.property} or a_static_value</my.property>
</properties>
</profile>
</profiles>
This might work for you:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugin>
<configuration>
<param>Foo</param>
</configuration>
</plugin>
</build>
...
</profile>
<profile>
<id>notdefault</id>
...
<build>
<plugin>
<configuration>
<param>${myProperty}</param>
</configuration>
</plugin>
</build>
...
</profile>
</profiles>
That way,
mvn clean will use "foo" as your default param. In cases when you need to override, use mvn -P notdefault -DmyProperty=something
I took sal's approach but flatten it a bit.
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugin>
<configuration>
<version>LATEST</version>
</configuration>
</plugin>
</build>
</profile>
</profiles>
Now you have 2 options:
Using default value: MVN install (all $version will be replaced with LATEST)
Using own value: MVN install -P! Default -Dversion=0.9 (all $version will be 0.9)
I am working on a spring-boot application. I have two profiles inside my POM, but when I am trying the build the project using clean install -Pdev its not reflecting the change in application.properties, it'll only reflect when I am using 'activeByDefault' tag in one of the profile.
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
</profile>
<profile>
<id>release</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<activatedProperties>release</activatedProperties>
</properties>
</profile>
</profiles>
If I am running clean install -Pdev I am getting this in my application.properties.
activatedProperties=#activatedProperties#
If I am setting the <activeByDefault>true</activeByDefault> then I'll get the value inside application.properties.
activatedProperties=release.
The frustration is I am not able to use other profiles.
Add in your application.properties
spring.profiles.active=#activatedProperties#
If Maven doesn't find the directory which contains your application.properties file in runtime, you need to setup the Maven Resources Plugin to filter the directory.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
Or just add what profile you want to active in application.properties
spring.profiles.active=dev
NOTE: The Maven profile and the Spring profile are different things.
In my pom.xml file, I have multiple profiles set up. I would like to use the current profile's values in my application.conf file. The Ninja Framework documentation only mentions mode configurations, however I can't find anything concerning profile configurations.
An example: The documentation mentions
database.name=database_production # will be used when no mode is set (or prod)
%prod.database.name=database_prod # will be used when running in prod mode
%dev.database.name=database_dev # will be used when running in dev mode
%test.database.name=database_test # will be used when running in test mode
How would I be able to set different database names depending on the currently used profile?
You could add whatever it is you want replaced as a property to your profile definition, and then enable maven resource filtering like this:
<build>
..
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
..
</build>
<profiles>
<profile>
<id>developmentProfile</id>
<properties>
<dbName>database.name=database_dev</dbName>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>productionProfile</id>
<properties>
<dbName>database.name=database_prod</dbName>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
Be careful, if you use spring-boot, you should reference the properties in the application.properties file like this for it to work:
someOtherProp = something
#dbName#
After that, building the app should filter the property correctly:
mvn clean install -PdevelopmentProfile
Result:
someOtherProp=something
database.name=database_dev
we have a project which initially was developed to run on a linux platform, we want to customize it and use it under windows platform. It is hosted on github. Any advice on how to manage files that are shared between the two platforms but have some specificity for each one. for example configuration files that contains paths, environment variables and the like.
thanks for suggestion.
Since you have tagged maven here is the solution, use profiles to differentiate between linux and windows.
<profiles>
<profile>
<id>linux</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<envName>linux</envName>
</properties>
</profile>
<profile>
<id>windows</id>
<properties>
<envName>windows</envName>
</properties>
</profile>
</profiles>
<build>
<outputDirectory>${project.basedir}/target/classes</outputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.class</include>
</includes>
</resource>
<resource>
<targetPath>${project.basedir}/target/classes/configurations</targetPath>
<directory>${basedir}/src/main/resources/configurations/${envName}</directory>
</resource>
</resources>
<plugins>
</build>
Say suppose you have some configuration files specific to OS. and when you want to build based on the enivronment you want the file to copied to configurations directory inside class, the above code snippet copies that. For that to work you need to create 2 directories inside src/main/resoruces/configurations/linux and src/main/resources/configurations/windows. when you run maven you need to use profile like mvn package -Plinux or mvn package -Pwindows.