I have a lot of maven projects. I have two different global settings.xml for them which I need to merge. Both of them describes their repositories inside the profiles. I merged the two settings.xml by the mirrors and profiles tags.
One of the profiles is activated by the activeProfile tag in settings.xml file.
I want to ask you, how I can change the active profile in the different projects? Can I select it in the project pom.xml file?
Thanks,
Br, Stristi
This document describes how profiles can be activated: Introduction to Build Profiles.
You could activate profiles via environment variables, but if you have many projects, and want to apply profiles per-project basis, then it's a fault solution to change environment variable before each separate project build execution. If you are working with IDE, you can configure launch configuration to set the property for each project, and you have to configure the correct mvn run command within a continuous integration for each project. However, that would make it impossible to build an aggregated build consisting of different projects that should be activated by different profiles.
A possible workaround (which doesn't seem to be elegant, but I'm pretty sure it will work) is activating profiles based on file presence, and managing such 'marker files' for different projects to activate the required one. For example:
<profiles>
<profile>
<activation>
<file>
<exists>.profile-A</exists>
</file>
</activation>
...
</profile>
</profiles>
Expected behavior: This profile gets activated if file with a name '.profile-A' exists in a root folder of a project.
However, at this point one more interesting question arises: what would be the behavior in case of aggregated build?
It sounds you need to use a Repository Manager (Archiva, Artifactory, Nexus alphabetical order) to have a single configuration in your settings.xml file. This will solve the issue of making your build files environment depend.
I prefer this way in my projects:
<profile>
<id>dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
...
</profile>
Then you can activate it this way:
mvn your_goal -Denv=dev
Regards,
Boskop
you can simple add the activeByDefault tag to your maven profile:
<profiles>
<profile>
<id>your-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
Related
I have created a multi-module maven project and I want to have two profiles, which I've declared as below.
<profiles>
<profile>
<id>bootstrap-server</id>
<modules>
<module>./bootstrapserver</module>
</modules>
</profile>
<profile>
<id>file-search</id>
<modules>
<module>./filesearch</module>
<module>./webservice</module>
</modules>
</profile>
</profiles>
When I choose, for an example, file-search profile, IntelliJ builds the profile fine (Using integrated maven panel in IntelliJ) and it builds only the two modules I've specified, which is the required behaviour. It works well for the other profile as well.
However, when I choose one profile, the directories of the modules in other profiles remain in the project structure window, as displayed below.
The image above shows how IntelliJ shows the project structure when I activate file-search profile.
Can I configure IntelliJ to remove the modules displayed in the Project window which are not used in one profile? For an example, if I select file-search module, I want to remove bootstrapserver module from the Project panel since it's not being used when developing under file-search profile.
Thank you.
My application should work in 2 modes: standard and custom.
I am using the same classes but it can react little bit differently in different modes.
How better to inject this mode into all(not all but a lot) classes?
Application should be switched in run-time.
Not boot time.
I am using java8 and groovy.
Thanks!
Agree with Igor, probably spring profiles would be helpful for you. Also, I could suggest maven profiles (in case if you use maven as a build tool of course). For example, if you have two different modes "production" and "development" you can create two directories with property file props.properties and do the following with maven:
<profiles>
<profile>
<id>production</id>
<properties>
<resource.location>classpath:production</resource.location>
</properties>
</profile>
<profile>
<id>development</id>
<properties>
<resource.location>classpath:development</resource.location>
</properties>
</profile>
</profiles>
After this you are free to configure your spring property placeholer in this way
<context:property-placeholder location="${resource.location}/props.properties" ignore-unresolvable="true"/>
And build you app with
mvn install -Pdevelopment
mvn install -Pproduction
As per runtime switch, could you provide more details on your application. In case if it is a web application you could create some webservice for switching modes.
I have one situation where I need to create multiple configuration property file by passing environment.
i.e : I have environment like dev, prod, qa, int, but I want to create jar only for int and dev in one go
My First question is: "Is there any way in maven where we can pass multiple environment value" ?
If this is possible how can I setup my pom.xml to accept the multiple env value ?
It's not quite clear what you mean with "pass multiple environment value".
If you want to activate a set of properties depending on the execution environment you could use maven profiles.
Here is an example for two profiles, which you can add to a pom.xml.
<profiles>
<profile>
<id>prod</id>
<properties>
<value1>a</value1>
<value2>b</value2>
</properties>
</profile>
<profile>
<id>qa</id>
<properties>
<value1>x</value1>
<value2>y</value2>
</properties>
</profile>
</profiles>
You could then for example activate profile "prod" by calling maven with the paramter "-Pprod".
I fear you can't trigger 2 different packaging with per-profile filtered resources in one build.
If you want to activate 2 profile at once you can specify them both using -P or trigger the activation according to the same property (see Maven: Only execute plugin when a command line flag is present)
If I correctly understand your question, i'd suggest to delegate the build of the integration artifact to a continuous integration server which will trigger the appropriate resource filtering using a dedicated profile and to set the dev profile as default one for the daily developers builds.
I use this to build my spring mvc app:
mvn clean package
I use the maven war plugin to create a war file, but the problem I am facing is that in my resources folder I have my development versions of my .properties files for log4j etc.
When I push to production, and run:
java -jar ...
It explodeds the war file, and then at that point I can modify the .properties files with my production settings, but I obviously want to do this during my maven build for production.
Is there a way I can tell maven that this is a production build, so get these files from somewhere else? And during development, keep doing what it is doing now?
User maven profiles. Maven profiles help you in specifying different properties for different profiles. So you can have two profiles - development and production.
Something like this -
<profiles>
<profile>
<id>development</id>
<!-- we'll properties here... -->
</profile>
<profile>
<id>production</id>
<!-- ...and here -->
</profile>
</profiles>
Like this example -
<profile>
<id>development</id>
<properties>
<db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass>
<db.connectionURL>jdbc:oracle:thin:#127.0.0.1:1521:XE</db.connectionURL>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass>
<db.connectionURL>jdbc:oracle:thin:#134.0.0.1:3124:XE</db.connectionURL>
</properties>
</profile>
There are a couple of options here. The first (as others have mentioned as well) is to use maven profiles. Instead of having multiple version of your properties files, you would have something like:
mypropsfile.properties
-----------------------
prop1=${prop1.val}
prop2=${prop2.val}
Then in your profiles, you can define values for those properties (make sure you have resource filtering enabled for this to work. see an example http://www.manydesigns.com/en/portofino/portofino3/tutorials/using-maven-profiles-and-resource-filtering).
You can also have your properties file have production values in it but with the ability to override those files in development. Spring profiles are helpful for this. For example, in development mode you can look for a properties file named <user-home>/mypropertiesoverride.properties, which could be used to override any of the production value properties with development specific ones.
I prefer the second method here where you have a default properties file and then you can just override select properties.
I have following pom.xml
<project>
<properties>
<buildNumber>dev</buildNumber>
</properties>
<build>
<finalName>${project.artifactId}-${project.version}-${buildNumber}</finalName>
</build>
</project>
This works fine on development machine. If I run mvn package I've got project-1.1-dev.war artifact. If I run mvn package -DbuildNumber=121 I've got package-1.1-121.war.
But out CI server (TeamCity) always got project-1.1-dev.war despite the fact that buildNumber property passed to maven (if I remove default property definition from pom.xml, maven builds artifact with correct filename).
It seems that system property resolve priority is somehow depends on platform (maven version is equals on both developer machine and TC - 2.2.1)?
That's a bit strange... Maybe you can't force the parameter given in the command line to have a highest priority than the one defined in the <properties> tag.
An idea is to use a profile that define the property buildNumber:
<profiles>
<profile>
<id>dev-property</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<buildNumber>dev</buildNumber>
</properties>
</profile>
</profiles>
So by default, buildNumber will be equals to dev value. Now, in your TeamCity command line, disable this profile with the command mvn ... -P !dev-property (the ! before a profile id indicates that the profile must be disabled).
The answer to your question lies in another question here on SO: Maven property overloading
In short, you need to pass -DbuildNumber=121 on the maven command line (on the "3 build step: maven" page), because setting system property "buildNumber" on the "6 properties and variables" page does not override the maven property.
Are you familiar with the concept of SNAPSHOT versions? Sounds like what you are trying to do, and is supported out of the box by Maven. Looks like you want to build major-minor-incremental or major-minor-dev, if you can live with major-minor-incremental-SNAPSHOT instead it should do what you want.