I have a profile defined in parent pom.xml and activated by property. In child module I need to de-activate it. How this can be done (it's Maven 3)?
ps. As far as I know it can't be done legally (MNG-4565, MNG-5083), but what is a possible workaround?
It depends. Child poms should not deny parent pom behaviour (it's like LSP for build process). It may be a good idea to define profile in a parent pom, but activate it only in child poms where you really need it.
If you can't do it, you could activate profile in a parent pom by a property with exact value:
<profiles>
<profile>
<activation>
<property>
<name>environment</name>
<value>test</value>
</property>
</activation>
</profile>
</profiles>
and then redefine property value in a child pom.
Related
I have a parent/root POM that (1) aggregates child modules, and (2) declares a profile that is conditionally activated based on a property value. So:
<packaging>pom</packaging>
<modules>
<module>moduleA</module>
<module>moduleB</module>
<module>moduleC</module>
</modules>
<profiles>
<profile>
<id>myProfile</id>
<activation>
<property>
<name>animal</name>
<value>cat</value>
</property>
</activation>
</profile>
</profiles>
And in moduleA, I declare the same profile as the parent, and would like it to be activated when I build moduleA using the activation criteria from the parent (animal == cat). But that isn't working. So if this is moduleA's POM:
<properties>
<shape>circle</shape>
</properties>
<profiles>
<profile>
<id>myProfile</id>
<properties>
<shape>square</shape>
</properties>
</profile>
</profiles>
And I build moduleA using mvn help:evaluate -Danimal=cat, evaluating ${shape} says its value is circle, not square as I'd expect. So it seems like the child profile is not activating when building using its parent's activation criteria.
Is there a way to do this? Do I really have to copy and paste the <activation> block from the parent into all its children modules, just so I can get the same behavior?
Try repeating the activation criteria in moduleA. You shouldn't have to repeat other profile content from the parent, but the activation criteria need to match.
<profile>
<id>myProfile</id>
<activation>
<property>
<name>animal</name>
<value>cat</value>
</property>
</activation>
</profile>
Generally trying to share profile config from one module to another is tricky. There are other questions:
Activating a Child Profile from a Parent Profile
Why can't I activate a Maven2 profile from another profile?
I have declared some properties that are specific to Maven profiles. A part of my pom.xml:
<profiles>
<profile>
<id>release</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<my.properties.file>foo.xml</my.properties.file>
</properties>
</profile>
<profile>
<id>ci</id>
<properties>
<my.properties.file>bar.xml</my.properties.file>
</properties>
</profile>
</profiles>
I encounter some problem to use the "ci" Maven profile when I start Junit tests via IntelliJ IDEA 2016.
I activate my profile via the "Maven Projects" panel, then I start tests. The problem is the "my.properties.file" property value is equal to "foo.xml", not "bar.xml".
I have no problem with command-line (I can use the "-Pci" flag). How can I tell IntelliJ to use the "ci" profile? Thx.
You should add the profiles to the Maven setting.xml file (you should find it in the path ${YOUR_MAVEN_HOME}\apache-maven-3.1.1\conf\setting.xml).
Then, you have to open intellij, click on View > Tool Windows > Maven Projects. There, you should see your profiles (ci and release) and select the correct one.
Hope this can help you.
Just finally solved it.
<profile>
<id>profile-to-be-activated-on-build</id>
<activation>
<activeByDefault>false</activeByDefault><!-- on your flavor -->
<property>
<name>mvn-profile-env-var-trigger</name>
</property>
</activation>
</profile>
Goto JUnit default profile (aka configuration template). Add into JVM args:
-Dmvn-profile-env-var-trigger
You may need to manually reload maven profiles in IDE.
Also make sure on [Settings > Build Tools > Maven > Running tests] envVars is checked (or better everything).
I am familiar with configuring maven-compiler-plugin to use java 1.6 in pom.xml and using parent pom file.
Is there a way to configure java compiler level to java 1.6 in the settings.xml level (so that all my maven projects will use java 1.6)?
In this thread default maven compiler setting somebody told there is a way to configure it in settings.xml.
Can someone please explain how to configure?
PS: Another way to specify Java compiler level:
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
-Siva
Simple answer No. In the settings file you don't configure things like this, cause it doesn't make sense and would make your builds not reproducible. The information about configuring the compiler plugin in settings.xml is rubbish.
source/target level can be configured in settings.xml, like this:
<profiles>
<profile>
<id>jdk-1.6</id>
<activation>
// can be replaced with other conditions
<jdk>1.6</jdk>
</activation>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
I use maven to build my java based program. It is work fine. But now I meet an issue which request me to build a revision for other users which is based on the same source code with a little different(e.g. different software name, and different resource file).
Does anyone have any idea about how to do it?
You need to use profiles. It's a little complicated to explain in an answer like this, but essentially, you will create different profiles within your POM for the different builds you want to do. You will choose the profile at build time, using, e.g., a variable definition in the mvn command line, and within the profile, you change any of the variables or settings that you need to change. Lots more info is available here.
Depending on the type of project, the most "maven" way to do this would be to split up the project into a parent with multiple children. Your main project is put into one module and each of user specific configurations goes into a different module, which can then depend on the common code.
Each of the user specific modules can have their own resources and unique configuration, which would make producing different named configurations easier. It would also make any user specific coding tweaks easier.
You can create different profiles within your POM for the different builds you want to do.
Here are some of the example part of POM.xml
<!-- Define profiles here and make DEV as default profile -->
<profiles>
<!-- dev Profile -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- qa Profile -->
<profile>
<id>qa</id>
<properties>
<env>qa</env>
</properties>
</profile>
<!-- prod Profile -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
...
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
I have a multi module maven project, which builds successfully, I'd like to build just one of the modules I have. How would I do that with profiles ? I could do it from console in two ways, one way is go to the child module and mvn package or I could use reactor to build just one module.
Can I do the same thing with profiles? By modifying POM? Thank you
EDIT
If is impossible from POM, can I do it from settings.xml ?
To implement this with profiles, you could use two profiles, one <activeByDefault> with all modules and another one with the wanted module only. Something like this:
<profiles>
<profile>
<id>all</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>module-1</module>
...
<module>module-n</module>
</modules>
</profile>
<profile>
<id>module-2</id>
<modules>
<module>module-2</module>
</modules>
</profile>
<profiles>
And then call it like this:
mvn -Pmodule-2 package
Two things to note here:
You need to move the <modules> from the POM in a "default" profile (because <modules> from a profile are only additive, they do not hide the modules declared in the POM).
By marking it as <activeByDefault>, the "default" profile will be picked if nothing else is active but deactivated if something else is.
One could even parametrize the name of the module and pass it as property:
<profiles>
...
<profile>
<id>module-x</id>
<activation>
<property>
<name>module-name</name>
</property>
</activation>
<modules>
<module>${module-name}</module>
</modules>
</profile>
<profiles>
And invoke maven like this:
mvn -Dmodule-name=module-2 package
But this is a poor implementation IMHO, I prefer the -pl "advanced" reactor options (less xml, much more power and flexibility):
mvn -pl module-2 package
To overcome additivity nature of maven default <modules> working with <profiles>, you can use reactor with particular profile, e.g.:
mvn -pl module-2 -Pprofile-name package
This will package module-2 defined in profile-name and not in default profile.