Use settings.xml in Java - java

I have a Maven project and I tried to extract sensitive information from my code using ~/.m2/settings.xml. As far as I can tell from checking with the mvn -X command, the file can be read from.
This is my file:
<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>
<activeByDefault>true</activeByDefault>
<id>camera</id>
<properties>
<camera.url>some url</camera.url>
</properties>
</profile>
</profiles>
</settings>
I have tried getting the value from camera.url in my code using pom.xml:
<build>
<defaultGoal>install</defaultGoal>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<systemPropertyVariables>
<camera.url>${camera.url}</camera.url>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
And then I am trying to retrieve the value in code using System.getProperty("camera.url"), but this returns null.
I can't seem to figure out what I have done wrong.

Related

Java: set pom.xml property

I have Java Meven project and inside my pom.xml i have this property:
<properties>
<suiteXmlFile>testing.xml</suiteXmlFile>
<JAVA_1_8_HOME>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</JAVA_1_8_HOME>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>${JAVA_1_8_HOME}</executable>
</configuration>
</plugin>
So i case i am running my project from Windows i just type mvn test
In case i am with MACOS/Linux this path doesn't exist and i wonder what solutions can be found to fix this issue.
UPDATE
As suggestion here i add this profile:
<profile>
<id>platform-windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</executable>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>mac</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>/usr/bin/javac</executable>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Now how my code will now to run this particular id ?
You can make this configuration in two ways:
1) Explicit Profile
Open Maven pom.xml file available in your project directory:
<properties>
<suiteXmlFile>testing.xml</suiteXmlFile>
<JAVA_1_8_HOME>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</JAVA_1_8_HOME>
<JAVA_1_8_HOME_LINUX>/usr/java/jdk1.8.0_181/</JAVA_1_8_HOME_LINUX>
</properties>
<profiles>
<!-- Windows Profile-->
<profile>
<id>jdk-8-windows</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>${JAVA_1_8_HOME}</executable>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- Mac/Linux Profile-->
<profile>
<id>jdk-8-linux</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<fork>true</fork>
<executable>${JAVA_1_8_HOME_LINUX}</executable>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Default active profile was defined as: jdk-8-windows
If the main profile is Mac/Linux, use: <activeProfile>jdk-8linux</activeProfile>
To execute your Mac/Linux profile use: mvn test -P jdk-8-linux
2) Profile Activation via Maven Settings
Open Maven settings.xml file available in %USER_HOME%/.m2 directory where %USER_HOME% represents the user home directory. If settings.xml file is not there, then create a new one.
<settings>
[...]
<profiles>
[...]
<!-- Windows Profile-->
<profile>
<id>jdk-8-windows</id>
<properties>
<JAVA_1_8_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_8_HOME>
</properties>
</profile>
<!-- Mac/Linux Profile-->
<profile>
<id>jdk-8-linux</id>
<properties>
<JAVA_1_8_HOME_LINUX>/usr/bin/javac</JAVA_1_8_HOME_LINUX>
</properties>
</profile>
</profiles>
[...]
<activeProfiles>
<activeProfile>windows</activeProfile>
</activeProfiles>
</settings>
Default active profile was defined as: jdk-8-windows
If the main profile is Mac/Linux, use: <activeProfile>linux</activeProfile>
To execute your Mac/Linux profile use: mvn test -P jdk-8-linux
Reference:
Maven - Build Profiles
Compiling Sources Using A Different JDK
You could pass the property as a command line argument when running the mvn command, for example:
mvn test"-DJAVA_1_8_HOME=<OS specific path>"
For another solutions, take a look at maven condition based on os family
For profiles:
<project>
<profiles>
<profile>
<properties>
// Define profile specific properties here
</properties>
</profile>
</profiles>
</project>
After defining the profile specific properties, use them as you would use any other property.

Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.4.4:redeploy (deploy) on project duke tutoring

Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.4.4:redeploy (deploy) on project dukes-tutoring-war: Execution deploy of goal org.codehaus.cargo:cargo-maven2-plugin:1.4.4:redeploy failed: Failed to create deployer with implementation class org.codehaus.cargo.container.glassfish.GlassFish4xInstalledLocalDeployer for the parameters (container [id = [glassfish4x]], deployer type [installed]). InvocationTargetException: The container configuration directory "/home/yogesh/Downloads/glassfish4/glassfish4/glassfish/domains" does not exist. Please configure the container before attempting to perform any local deployment. Read more on: http://cargo.codehaus.org/Local+Configuration -> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
After correcting the problems, you can resume the build with the command
mvn <goals> -rf :dukes-tutoring-war
my pom.xml fromdukes-tutoring-war ---------
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>dukes-tutoring</artifactId>
<groupId>org.glassfish.javaeetutorial</groupId>
<version>7.0.5</version>
</parent>
<groupId>org.glassfish.javaeetutorial</groupId>
<artifactId>dukes-tutoring-war</artifactId>
<packaging>war</packaging>
<name>dukes-tutoring-war</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>dukes-tutoring-common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${maven.exec.plugin.version}</version>
<executions>
<execution>
<id>create-tutoring-realm</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<skip>false</skip>
<executable>/home/yogesh/Downloads/glassfish4/glassfish/bin/asadmin${glassfish.executables.suffix}</executable>
<arguments>
<argument>create-auth-realm</argument>
<argument>--classname</argument>
<argument>com.sun.enterprise.security.auth.realm.jdbc.JDBCRealm</argument>
<argument>--property</argument>
<argument>jaas-context=jdbcRealm:datasource-jndi='java:global/TutoringDataSource':user-table=tutoring.PERSON:user-name-column=email:password-column=password:group-table=tutoring.PERSON:group-name-column=DTYPE:digest-algorithm=none</argument>
<argument>tutoringRealm</argument>
</arguments>
<successCodes>
<successCode>0</successCode>
<successCode>1</successCode>
</successCodes>
</configuration>
</plugin>
</plugins>
</build>
</project>
The pom in examples folder for <glassfish.home.prefix> settings
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.glassfish.javaeetutorial</groupId>
<artifactId>javaeetutorial</artifactId>
<version>7.0.5</version>
<packaging>pom</packaging>
<name>javaeetutorial</name>
<scm>
<connection>scm:svn:https://svn.java.net/svn/javaeetutorial~svn/tags/javaeetutorial-7.0.0</connection>
<developerConnection>scm:svn:https://svn.java.net/svn/javaeetutorial~svn/tags/javaeetutorial-7.0.0</developerConnection>
</scm>
<issueManagement>
<system>IssueTracker</system>
<url>http://java.net/jira/browse/JAVAEETUTORIAL</url>
</issueManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javaee.api.version>7.0</javaee.api.version>
<maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
<maven.source.plugin.version>2.2.1</maven.source.plugin.version>
<maven.clean.plugin.version>2.5</maven.clean.plugin.version>
<maven.war.plugin.version>2.3</maven.war.plugin.version>
<maven.acr.plugin.version>1.0</maven.acr.plugin.version>
<maven.ear.plugin.version>2.8</maven.ear.plugin.version>
<maven.ejb.plugin.version>2.3</maven.ejb.plugin.version>
<maven.jar.plugin.version>2.4</maven.jar.plugin.version>
<maven.rar.plugin.version>2.3</maven.rar.plugin.version>
<maven.license.plugin.version>1.10.b1</maven.license.plugin.version>
<maven.release.plugin.version>2.4.1</maven.release.plugin.version>
<maven.exec.plugin.version>1.2.1</maven.exec.plugin.version>
<junit.version>4.11</junit.version>
<eclipselink.version>2.5.0</eclipselink.version>
<glassfish.embedded.version>4.0</glassfish.embedded.version>
<cargo.plugin.version>1.4.4</cargo.plugin.version>
<glassfish.domain.name>domain1</glassfish.domain.name>
<glassfish.home>${glassfish.home.prefix}/glassfish4</glassfish.home>
<integration.container.id>glassfish4x</integration.container.id>
</properties>
<profiles>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<glassfish.home.prefix>c:/</glassfish.home.prefix>
<glassfish.executables.suffix>.bat</glassfish.executables.suffix>
</properties>
</profile>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<glassfish.home.prefix>/home/yogesh/Downloads/glassfish4</glassfish.home.prefix>
<glassfish.executables.suffix />
</properties>
</profile>
<profile>
<id>sdk</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<glassfish.home>${basedir}/../../../</glassfish.home>
</properties>
</profile>
<profile>
<id>development</id>
<activation>
<file>
<exists>${basedir}/../bundle</exists>
</file>
</activation>
<properties>
<glassfish.home>${glassfish.home.prefix}/glassfish4</glassfish.home>
</properties>
</profile>
<profile>
<id>standalone</id>
<properties>
<glassfish.home>${basedir}/target/cargo/installs/glassfish</glassfish.home>
<cargo.maven.containerUrl>http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/latest-glassfish.zip</cargo.maven.containerUrl>
</properties>
</profile>
</profiles>
<modules>
<module>archetypes</module>
<module>batch</module>
<module>case-studies</module>
<module>cdi</module>
<module>concurrency</module>
<module>connectors</module>
<module>ejb</module>
<module>jaxrs</module>
<module>jaxws</module>
<module>jms</module>
<module>persistence</module>
<module>security</module>
<module>web</module>
</modules>
<repositories>
<repository>
<id>snapshot-repository.java.net</id>
<name>Java.net Snapshot Repository for Maven</name>
<url>https://maven.java.net/content/repositories/staging/</url>
<layout>default</layout>
</repository>
<repository>
<id>releases-repository.java.net</id>
<name>Java.net releases Repository for Maven</name>
<url>https://maven.java.net/content/repositories/releases/</url>
<layout>default</layout>
</repository>
</repositories>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven.source.plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>${maven.clean.plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven.war.plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>${cargo.plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<inherited>true</inherited>
<executions>
<execution>
<id>deploy</id>
<phase>integration-test</phase>
<goals>
<goal>redeploy</goal>
</goals>
</execution>
</executions>
<configuration>
<container>
<containerId>${integration.container.id}</containerId>
<type>installed</type>
<home>${glassfish.home}</home>
</container>
<configuration>
<type>existing</type>
<home>${glassfish.home}/glassfish/domains</home>
<properties>
<cargo.glassfish.domain.name>${glassfish.domain.name}</cargo.glassfish.domain.name>
<!--cargo.remote.username></cargo.remote.username-->
<cargo.remote.password />
</properties>
</configuration>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>com.mycila.maven-license-plugin</groupId>
<artifactId>maven-license-plugin</artifactId>
<version>${maven.license.plugin.version}</version>
<configuration>
<header>common/license.txt</header>
<excludes>
<exclude>common/**</exclude>
<exclude>**/META-INF/**</exclude>
<exclude>**/WEB-INF/**</exclude>
<exclude>**/nbactions.xml</exclude>
<exclude>**/nb-configuration.xml</exclude>
<exclude>**/glassfish-resources.xml</exclude>
<exclude>**/simple-flow-flow.xml</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>${maven.release.plugin.version}</version>
<configuration>
<!--
During release:perform, enable the "sdk" profile
-->
<releaseProfiles>sdk</releaseProfiles>
<autoVersionSubmodules>true</autoVersionSubmodules>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee.api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
my glassfish4 location: /home/yogesh/Downloads/glassfish4
apache maven location: /opt/apache-maven-3.3.9
I had a similar issue when I tried to build the hello1 project, and this was mainly due to the fact that I installed Netbeans with glassfish: I suppose you did the same thing.
I'm not too sure, but it appears installing Netbeans with glassfish causes some path problems when attempting to build projects.
This is what I did to resolve it on my openSUSE Leap computer:
Stop the glassfish server if it's running and close your Netbeans IDE
Open a terminal and uninstall glassfish
cd /path/to/glassfish/installation/directory (e.g: cd ~/glassfish-4.1.1)
./uninstall.sh
Download Java EE 7 SDK Update 2 (or the latest update) from http://www.oracle.com/technetwork/java/javaee/downloads/index.html
Unzip the downloaded file (this creates the glassfish4 directory)
cd /path/to/downloaded/file/directory
unzip java_ee_sdk-7u2.zip
At this stage, the glassfish server is already installed, but it's not yet integrated in the Netbeans IDE (You will find out that it is not listed as a server in the Netbeans IDE). Instructions on how to add glassfish server as a server in Netbeans IDE can be found here: https://docs.oracle.com/cd/E19798-01/821-1770/gioew/
You can now open Netbeans IDE and try to build your project again; you should have BUILD SUCCESS as I did.
I really hope this helps.
This might also relate to the version of you glassfish server, I solved the same problem by switching to glassfish4.1.
this line in the exception message:
(container [id = [glassfish4x]], deployer type [installed])
is probably searching for the glassfish4 version,
I had this issue with guessnumber-jsf example, but it was fixed by starting my glassfish server.

Using Maven WAR Plugin with Profiles

I'm trying to get images to my Maven web-application according to the profile selected at build time. But I keep on getting the whole folder of all the profiles to the point where I need the images to go.
<build>
<finalName>user-interface</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<webResource>
<directory>src/main/profile/webapp/</directory>
<targetPath>images</targetPath>
</webResource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>user</id>
</profile>
<profile>
<id>dev</id>
</profile>
</profiles>
How to fix this? Default image is banner.jpg and both profiles have respective images in correct order. Inside src/main/profile/webapp/ there are 2 folders user and dev with each having images folder and the banner.jpg inside images folder.
You need to define a custom property inside each profile and then reference that property in the configuration of the plugin:
<build>
<finalName>user-interface</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<webResource>
<directory>src/main/profile/webapp/${banner.folder}</directory>
<targetPath>images</targetPath>
</webResource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>user</id>
<properties>
<banner.folder>user</banner.folder> <!-- defines banner.folder property when the user profile is activated -->
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<banner.folder>dev</banner.folder> <!-- defines banner.folder property when the dev profile is activated -->
</properties>
</profile>
</profiles>

Conditionally adding build extension [duplicate]

My question had been addressed in this thread, but the explanation is not clear.
I have this build definition in one of my pom.xml files:
<build>
<finalName>${my.project}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.kuali.maven.wagons</groupId>
<artifactId>maven-s3-wagon</artifactId>
<version>1.1.19</version>
</extension>
</extensions>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/settings.properties</include>
</includes>
</resource>
</resources>
</build>
Notice that I'm using the maven-s3-wagon extension.
Next, I would like to have 2 different profiles, each with it's own settings, plugins and extensions but maven does not allow the extensions tag under a profile.
When I try using a profile:
<profiles>
<profile>
<id>local-build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<finalName>${my.project}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.kuali.maven.wagons</groupId>
<artifactId>maven-s3-wagon</artifactId>
<version>1.1.19</version>
</extension>
</extensions>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/settings.properties</include>
</includes>
</resource>
</resources>
</build>
</profile>
</profiles>
I get a an error in my pom:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'extensions'. One of '{"http://maven.apache.org/POM/4.0.0":defaultGoal, "http://maven.apache.org/POM/
4.0.0":resources, "http://maven.apache.org/POM/4.0.0":testResources, "http://maven.apache.org/POM/4.0.0":directory, "http://maven.apache.org/POM/4.0.0":filters, "http://
maven.apache.org/POM/4.0.0":pluginManagement}' is expected.
Question So using the extension tag means I can't use profiles? How can I use or change build extensions via profile?
Indeed, the official Maven POM reference is not clear about the possible usage of extensions as part of a Maven profile, since it states you can have a build element within it, but not what of the build section.
However, the official Maven model effectively filters and provides what of the build section you can actually use within a profile section. And indeed extensions is not there.
However, what are Maven extensions? Build/Lifecycle enhancement, but also (and essentially): a library added to the runtime classpath of the Maven build, which participates to the build, but it is not packaged with the final artifact.
Hence, in such a scenario (if you need to have extensions in profile or have a profile to change/add an extension) you could use the following trick:
Have an harmless extension as default extension of your build (where harmless means whatever library which could be part of your build classpath and essentially not affect it at all)
Have properties defining the GAV coordinates (GroupId, ArtifactId, Version) of this extension
Have a profile which overrides these properties with the desired (useful) extension
As an example, given the following sample POM:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<extension.groupId>junit</extension.groupId>
<extension.artifactId>junit</extension.artifactId>
<extension.version>4.11</extension.version>
</properties>
<build>
<extensions>
<extension>
<groupId>${extension.groupId}</groupId>
<artifactId>${extension.artifactId}</artifactId>
<version>${extension.version}</version>
</extension>
</extensions>
</build>
<profiles>
<profile>
<id>customize-extension</id>
<properties>
<extension.groupId>junit</extension.groupId>
<extension.artifactId>junit</extension.artifactId>
<extension.version>4.12</extension.version>
</properties>
</profile>
</profiles>
</project>
The default build (without the customize-extension profile activated), would use the default defined properties and as such add junit as build extension: this is harmless (although it may create conflicts with another junit version of your build, so make sure you use the same version of use an even more harmless library for that).
You can check Maven will pick it up by running a really first build phase, just to check information in our case, and enable the debug flag:
mvn initialize -X
And checking as part of the build log:
[DEBUG] Populating class realm extension>junit:junit:4.11
[DEBUG] Included: junit:junit:jar:4.11
Now let's use our trick: let's add (change) a build extension via profile:
mvn initialize -X -Pcustomize-extension
And as part of our build log we would have:
[DEBUG] Populating class realm extension>junit:junit:4.12
[DEBUG] Included: junit:junit:jar:4.12
Bingo. Maven picked up a different extension (in this case, a different version, the 4.12) and we succeeded on changing (or actually adding a meaningful) build extension via profile.
Just a crazy idea: use modules
Define a parent pom like this:
<groupId>org.example</groupId>
<artifactId>my-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<profiles>
<profile>
<id>use-pom1</id>
<modules>
<module>pom1</module>
</modules>
</profile>
<profile>
<id>use-pom2</id>
<modules>
<module>pom2</module>
</modules>
</profile>
</profiles>
Define the desired extensions on pom1 and pom2.
I think the solution is here http://maven.apache.org/guides/mini/guide-using-extensions.html
Define a build section where extensions are defined and then into the profile set the attribute true ( like in the second profile shown below )
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.9</version>
</extension>
</extensions>
</build>
<profiles>
<profile>
<id>create-default</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>build</name>
<value>full</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>create-core</id>
<activation>
<property>
<name>build</name>
<value>full</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<extensions>true</extensions>
<version>2.6</version>
<configuration>
<finalName>import-station-core-${project.version}</finalName>
</configuration>
<executions>
<execution>
<id>make-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

Maven , change in order of profile results different build directory struture

I am facing very ridiculous problem here i have a pom.xml which is being used to build the war file .I have introduced profile to work it for different environment(dev/prod) .but the problem is when i create build it create correct build directory for the profile which is below in order in pom.xml
Please assist what is the issue here.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<activation>
<property>
<name>lifecycle</name>
<value>prod</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<warSourceExcludes>**login.jsp</warSourceExcludes>
</configuration>
</plugin>
</plugins>
<directory>target/${lifecycle}</directory>
<finalName>testapp</finalName>
<resources>
<resource>
<directory>src/main/resources/${lifecycle}</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<activation>
<property>
<name>lifecycle</name>
<value>dev</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
</plugins>
<directory>target/${lifecycle}</directory>
<finalName>testapp</finalName>
<resources>
<resource>
<directory>src/main/resources/${lifecycle}</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
Correct build directory should be
if -Dlifecycle=dev
target/dev/testapp/
if -Dlifecycle=prod
target/prod/testapp/ but here i get 'target/testapp-xx.x'
why is this different behavior?
Command to trigger the build:mvn clean install -Dlifecycle=prod
Adding an id element to the prod profile fix this.
One thing you can do is specify which profile should be built.
Define an ID for each profile:
<profiles>
<profile>
<id>dev</id>
...
<profile>
When building, use the argument -P to inform maven which profiles to activate
mvn clean install -P dev

Categories

Resources