How do I define maven system properties in settings.xml? - maven

I am following advice from the mvn project to use multiple artifact threads for parallel artifact resolution. This command seems to give me the desired result.
mvn -Dmaven.artifact.threads=10 dependency:resolve-plugins
Will this settings.xml automatically set maven.artifact.threads on every call to mvn?
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<profiles>
<profile>
<id>moreDependencyThreads</id>
<activation>
<property>
<name>maven.artifact.threads</name>
<value>10</value>
</property>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
<activeProfiles>
<activeProfile>moreDependencyThreads</activeProfile>
</activeProfiles>
</settings>

You can set this property in your settings.xml file by changing it as follows:
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<profiles>
<profile>
<id>moreDependencyThreads</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<maven.artifact.threads>10</maven.artifact.threads>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>moreDependencyThreads</activeProfile>
</activeProfiles>
</settings>

As it also specified in tutorial, you can make it permanent via:
export MAVEN_OPTS=-Dmaven.artifact.threads=10
If you call that it will be permanent for that session, if you add tis to your ~/.bash_profile it will be permanent for that user. Now on every call maven will work with these options.

Related

Maven - Profile Depending on Property?

I have the following profile below, instead of using activeByDefault I would like to active it through a property instead. I saw this comment (https://stackoverflow.com/a/18890159/1959534) in passing about being able to do so through maven assembly plugin but I couldn't figure out how to specify that. Any input on how to accomplish this?
<project ...>
...
<profiles>
<profile>
<id>myProfile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<!-- Extra dependency just for this profile -->
</dependency>
</dependencies>
</profile>
</profiles>
</project>
Figured it out!
Defined a system variable in the project's /.mvn/jvm.config and then changed the activation step to be through property instead of activeByDefault. Then used this tips to throw config out to my .properties file: https://stackoverflow.com/a/14386303/1959534
jvm.config
-Dmy.property=true
pom.xml
<profiles>
<profile>
...
<activation>
<property>
<name>my.property</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>

Is it possible to add MVN option in CLI?

Is it possible to add to run mvn as command with profile or Repository or other element and not from settings.xml ?
for example
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<localRepository>/home/vcap/app/.java-buildpack/tomcat/temp/maven_repo_6194547201292702175</localRepository>
<interactiveMode>false</interactiveMode>
<profiles>
<profile>
<repositories>
<repository>
<id>public.nexus</id>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</profile>
</profiles>
<activeProfiles>
<activeProfile>public.nexus</activeProfile>
</activeProfiles>
</settings>

how to set a property value according to profile

I'm new to maven and i got this issue when sometimes when i run maven i want to have a property with 1 value and sometimes the other.
I tried to do the following but still something is missing:
<profiles>
<profile>
<id>production</id>
<activation>
<property>
<name>com.sencha.cmd.dir</name>
<value>
${env.SENCHA_PATH}\senchaCmd-${com.sencha.cmd.version}\Sencha\Cmd\${com.sencha.cmd.version}
</value>
</property>
</activation>
</profile>
</profiles>
<properties>
<com.sencha.cmd.dir> ${env.SENCHA_PATH}\senchaCmd- ${com.sencha.cmd.version}\Sencha\Cmd\${com.sencha.cmd.version}</com.sencha.cmd.dir>
my question is how to replace the value of the property com.sencha.cmd.dir according to the availability of the profile?
Hope this is clear enough
It's really simple
<profiles>
<profile>
<id>profile 1</id>
<activation>
...
</activation>
<properties>
<my.property> xxx </my.property>
</properties>
</profile>
<profile>
<id>profile 2</id>
<activation>
...
</activation>
<properties>
<my.property> yyy </my.property>
</properties>
</profile>
</profiles>
and then you can use your my.properties outside, even in an other one properties
<properties>
<my.next.property> abc ${my.property} def </my.next.property>
</properties>

Using profiles to control which Maven modules are built

I have the following XML in my maven POM.xml:
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>default</name>
<value>!disabled</value>
</property>
</activation>
<modules>
<module>m1</module>
<module>m2</module>
<module>m3</module>
</modules>
</profile>
<profile>
<id>x</id>
<modules>
<module>m1</module>
</modules>
</profile>
</profiles>
What I'm trying to achieve is this:
When I run mvn install, I want it to build m1, m2 and m3 projects.
When I run mvn install -Px, I want it to only build m1.
My current problem is that with the code above, option 2 builds all m1, m2 and m3.
Found the solution guys, define 'x' profile first and the 'default' and it works fine (insane Maven!!). Here's the final result:
<profiles>
<!-- DO NOT CHANGE THE *ORDER* IN WHICH THESE PROFILES ARE DEFINED! -->
<profile>
<id>x</id>
<modules>
<module>m1</module>
</modules>
</profile>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>m1</module>
<module>m2</module>
<module>m3</module>
</modules>
</profile>
</profiles>
You can disable maven profiles that have runByDefault set to true from the command line like so:
mvn install -P !default
Note, this requires Maven version 2.0.10.
Just add a space after -P the sintax of the command is
mvn install -P x
And not like you are using
mvn install -Px
Take a look at Maven - Introduction to profiles

Triggering Maven profiles from Heroku configured Environment Variables

I am trying to activate a profile in my maven pom as follows:-
<profile>
<id>test</id>
</properties>
<activation>
<property>
<name>env.SITE</name>
<value>test</value>
</property>
</activation>
</profile>
Now i am configuring heroku to have the environment variable SITE as follows:-
heroku config:add SITE=test.
I expect the environment variable to trigger the profile when code is pushed. However this is not happening.
You can do this without a custom build pack.
Use this snippet in your pom.xml to display all properties available on Heroku and pick one that is not in your local: http://florianlr.wordpress.com/2012/04/24/16/
I used env.DYNO
<profile>
<id>heroku</id>
<activation>
<property>
<name>env.DYNO</name>
</property>
</activation>
...
</profile>
...
Works like a charm:)
You can specify the maven profile using the environment variable MAVEN_CUSTOM_OPTS.
In your pom define maven profile activation to be based off the presence of a property. The below example uses the development profile if you specify it and defaults to production if you don't. reference
<profiles>
<profile>
<id>production</id>
<activation>
<property>
<name>!profile-development</name>
</property>
</activation>
</profile>
<profile>
<id>development</id>
<activation>
<property>
<name>profile-development</name>
</property>
</activation>
</profile>
</profiles>
In Heroku set the MAVEN_CUSTOM_OPTS environment variable to use the profile. Note that this variable overrides the default option -DskipTests. reference
MAVEN_CUSTOM_OPTS=-DskipTests -Dprofile-development
In this example the production profile would not need MAVEN_CUSTOM_OPTS declared.
Or you can introduce your own customized Maven settings.xml file, e.g. heroku-settings.xml:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- activate by setting the MAVEN_SETTINGS_PATH config var to heroku-settings.xml in Heroku project settings tab.
See https://devcenter.heroku.com/articles/using-a-custom-maven-settings-xml for more details.
-->
<activeProfiles>
<activeProfile>production</activeProfile>
</activeProfiles>
</settings>
Then activate the settings by setting the MAVEN_SETTINGS_PATH config var to heroku-settings.xml in Heroku project settings tab
The config vars aren't currently available at compile time. To change the Maven profile you will need to fork the Heroku Java Buildpack.
I solved my problem by adding Heroku Environment like below:
MAVEN_CUSTOM_OPTS=-P<profile_id>
and cut out "activation" tag from pom.xml.
Btw I don't know why this doesn't work (in Heroku I have ENVIRONMENT=test)
<profile>
<id>test</id>
<activation>
<property>
<name>env.ENVIRONMENT</name>
<value>test</value>
</property>
</activation>
...
</profile>

Resources