Accessing Project Properties from settings.xml - maven

I'm trying to activate a profile in my settings.xml by the value of the pom's groupId. To achieve that I have:
<settings>
<profiles>
<profile>
<id>imf</id>
<activation>
<property>
<name>project.groupId</name>
<value>myId</value>
</property>
</activation>
<properties>
. . .
</properties>
</profile>
</profiles>
</settings>
But this doesn't work. Is it not possible to access project properties from settings? The reference material says you can.
Just to verify my use of the property activator element, I conducted a sanity check using a property set from the command line. Indeed, if I pass in -Dproject.groupId=myId to the mvn command line, my activation works. This leads me to believe that the project properties simply aren't available in the settings.xml file.

It looks like your specific requirement cannot be achieved in the way you have tried.
project.groupId as a property name (or key) does not mean anything to maven. maven understands (and expands) ${project.groupId} and similar values in settings.xml or pom.xml.

I had a similar requirement - choosing between profiles depending on the groupId of the project (to choose between remote repositories). Then I noticed that my groupIds are always actually package names in my java class hierarchy. And package names are just simply directories in the filesystem. So my solution to this problem was to use the actication/file/exists tag:
<profiles>
<profile>
<id>client1</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${basedir}/src/main/java/hu/client1</exists>
</file>
</activation>
<repositories>
<repository>
</repository>
</repositories>
</profile>
<profile>
<id>client2</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${basedir}/src/main/java/hu/client2</exists>
</file>
</activation>
<repositories>
<repository>
</repository>
</repositories>
</profile>
<profile>
<id>client3</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${basedir}/src/main/java/com/client3</exists>
</file>
</activation>
<repositories>
<repository>
</repository>
</repositories>
</profile>
</profiles>

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>

Maven build deploy target throwing exception for Sonatype Nexus Repository

I am using the following snippet in my pom to deploy the artifact. It was working till now and started failing recently.
The same build file is working fine when I kick off the build from Jenkins Linux box.
<profile>
<id>snapshots-repository</id>
<activation>
<property>
<name>!useReleaseRepo</name>
</property>
</activation>
<properties>
</properties>
<distributionManagement>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>https://${nexus.instance}.xxx.com:8443/repository/maven-snapshots/</url>
</repository>
</distributionManagement>
</profile>
<profile>
<id>prod-release-repository</id>
<activation>
<property>
<name>useReleaseRepo</name>
<value>true</value>
</property>
</activation>
<properties>
</properties>
<distributionManagement>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>https://${nexus.instance}.xxx.com:8443/repository/maven-releases/ </url>
</repository>
</distributionManagement>
</profile>
Error:
Failed to deploy artifacts/metadata: Cannot access https://nexus.com:8443/repository/maven-snapshots/ with
type default using the available connector factories: BasicRepositoryConnectorFactory: Cannot access https://nexus.com:8443/repository/maven-snapshots/ using the registered transporter factories: WagonTransporterFactory: java.util.NoSuchElementException
I'm looking for a recommendation for what needs to be changed to make it work again. I didn't change my .m2/settings file also.

Maven add a repository for a specific OS

I am new to maven and I am trying to create a single pom file for both linux and windows. For dependency resolution, I have 2 p2 repositories. 1 for windows and the other is for linux.
I also have a target definition (.target) in which I am adding all external repositories for dependecy resolution.
Is there a way that I can add/activate a repository location per OS? Below is my trial.
Using maven pom profiles:
In this trial, I added all my repositories (common for windows and linux) to the target definition. I then created 2 new profiles and added the OS-specific p2 repo like so
<profile>
<id>Linux</id>
<activation>
<activeByDefault>false</activeByDefault>
<os><name>linux</name></os>
</activation>
<repositories>
<repository>
<id>p2_lin</id>
<url>http://example.com/data/LINUX_P2</url>
<layout>p2</layout>
</repository>
</repositories>
</profile>
<profile>
<id>Windows</id>
<activation>
<activeByDefault>false</activeByDefault>
<os><name>win32</name></os>
</activation>
<repositories>
<repository>
<id>trace_p2_win</id>
<url>http://example.com/data/WINDOWS_P2</url>
<layout>p2</layout>
</repository>
</repositories>
</profile>
While calling maven, I add this profile to the parameters mvn clean install -PWindows,Test
Unfortunately, the above configuration gives an unsatisfied dependency.
But when I remove this section from the parent pom file and add the correct repository (example: >http://example.com/data/WINDOWS_P2) to the target definition without the other one, everything seems to be fine.

Different SCM for different profiles in Maven

In my project we have to use maben-build-number plugin to construct the final name of the jar, for that purpose we use the revision of the SCN, so we need SCM
But we have two SVN on controlled environment without direct access and on our local test environment, so for our pouproses we have to use:
<scm>
<connection>scm:svn:http://dev.com/svn_repo/trunk</connection>
<developerConnection>scm:svn:https://dev.com/svn_repo/trunk</developerConnection>
<url>http://dev.com/view.cvs</url>
</scm>
But for client env:
<scm>
<connection>scm:svn:http://client.com/svn_repo/trunk</connection>
<developerConnection>scm:svn:https://client.com/svn_repo/trunk</developerConnection>
<url>http://client.com/view.cvs</url>
</scm>
Is it possible to configure this in the different profiles. I tried
<profiles>
<profile>
<id>local</id>
<scm>
<connection>scm:svn:http://client.com/svn_repo/trunk</connection>
<developerConnection>scm:svn:https://client.com/svn_repo/trunk</developerConnection>
<url>http://client.com/view.cvs</url>
</scm>
</profile>
<profile>
<id>remote</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<scm>
<connection>scm:svn:http://client.com/svn_repo/trunk</connection>
<developerConnection>scm:svn:https://client.com/svn_repo/trunk</developerConnection>
<url>http://client.com/view.cvs</url>
</scm>
</profile>
</profiles>
But when I use the profile -Plocal, nothing happens?
I solved this problem by creating a property for the SCM connection details I need to change for different profiles. Here is a summary of what I did.
Defined the property and it's default value the properties block.
<properties>
<developerConnectionUrl><!-- developerConnection url here --></developerConnectionUrl>
</properties>
Set the scm block properties to use the global properties.
<scm>
<developerConnection>${developerConnectionUrl}</developerConnection>
</scm>
Created a profile which changes the global property as required.
<profiles>
<profile>
<id>local</id>
<properties>
<developerConnectionUrl><!-- developerConnectionUrl url for profile --></developerConnectionUrl>
</properties>
</profile>
</profiles>
As far as I know and from a quick scan of the schema the scm tag can only be used at the top level in the POM and is not valid inside the profile tag

maven settings custom properties

I'm completely new to Maven. I'm trying to set up a new project such that it doesn't require 20 page long word docs with screenshots to set it up. I've got developers on Macs and PCs, so I need to be able to customize the catalina.home directory as I can't just impose a standard location. I thought I'd do it in the ~/.m2/settings.xml file with the following:
<profile>
<id>my-site-dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<catalina.home>/path/to/apache-tomcat</catalina.home>
</properties>
</profile>
However, the documentation here: http://maven.apache.org/guides/introduction/introduction-to-profiles.html#Profile_Pitfalls seems to indicate that what I'm trying to accomplish is a bad idea. What's the official way of accomplishing this so that in my pom.xml I can just reference ${catalina.home}?
And now that I've declared my profile with an id, can I fail the build if my pom.xml can't load the profile "my-site-dev"? I'd like to get that <activeByDefault> out of the settings.xml if at all possible. I don't want to interfere with their global settings for no reason, I'd like to keep as much of it self contained as possible.
Don't use activeByDefault, since it will cloak all other profiles:
Here is what you should use:
<profiles>
<profile>
<id>path-to-catalina.home-linux</id>
<activation>
<os>
<family>linux</family>
</os>
</activation>
<properties>
<catalina.home>...</catalina.home>
</properties>
</profile>
<profile>
<id>path-to-catalina.home-mac</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<properties>
<catalina.home>...</catalina.home>
</properties>
</profile>
<profile>
<id>path-to-catalina.home-windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<catalina.home>...</catalina.home>
</properties>
</profile>
You could use a profile in your pom that gets activated based on os.
<profiles>
<profile>
<activation>
<os>
...
What are you doing that require catalina.home?
If you're doing deployment to an app server with maven, you are already outside of the 'consistent, portable build' rule set - and outside of the main use case / best practices for maven.
Not that you shouldn't do it - as it's convenient. Just make sure to separate your build projects from the deployment projects.

Resources