I want to generate two artifacts (.ear) for one profile with two maven builds. I need that the name of the artifact doesn't contain the project version, I can't do it this way.
I'm trying multiple solutions but none has served to me.
I tried to include the tag <outputFileNameMapping>#{artifactId}#.#{extension}#</outputFileNameMapping>
or the tag
<fileNameMapping>no-version</fileNameMapping>
to the configuration tag.
I tried to change the maven-ear-plugin version too, with the same result.
This is my configuration:
<finalName>${area}${project.parent.artifactId}</finalName>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>ear-prepro</id>
<configuration>
<finalName>${project.parent.artifactId}finalll</finalName>
<outputFileNameMapping>#{artifactId}#.#{extension}#</outputFileNameMapping>
<earSourceDirectory>src/main/resources</earSourceDirectory>
<displayName>${area}${artifactId}</displayName>
<fileNameMapping>no-version</fileNameMapping>
<outputDirectory>${project.build.directory}</outputDirectory>
<modules>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>${project.parent.artifactId}-web</artifactId>
<bundleFileName>${project.parent.artifactId}-web.war</bundleFileName>
<contextRoot>/${project.parent.artifactId}</contextRoot>
</webModule>
</modules>
</configuration>
<goals>
<goal>ear</goal>
</goals>
</execution>
</executions>
</plugin>
--------------------------
AND THE OTHER EAR CONFIGURATION
----------------------------
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>ear-protest</id>
<configuration>
<fileNameMapping>no-version</fileNameMapping>
<finalName>${area}${project.parent.artifactId}-PROTEST</finalName>
<earSourceDirectory>src/main/resources</earSourceDirectory>
<displayName>${area}${artifactId}-PROTEST</displayName>
<outputDirectory>${project.build.directory}/PROTEST</outputDirectory>
<modules>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>${project.parent.artifactId}-web</artifactId>
<bundleFileName>${project.parent.artifactId}-web.war</bundleFileName>
<contextRoot>/${project.parent.artifactId}</contextRoot>
</webModule>
</modules>
</configuration>
<goals>
<goal>ear</goal>
</goals>
</execution>
</executions>
</plugin>
It seems that the tag concats the version number to the artifact, but I don't need it. I need the name of the artifact without the project version on the .war file.
Some ideas?
Related
Is there any sort of Maven plugin that allows me to copy the class files for a dependency into the target\classes folder for my project? Currently I have to manually open the jar file that has the dependencies extract the package that I need to copy over and then copy it into the target\class folder for my project.
I need a way to do those ideally within the pom.xml file but I haven't been able to find a solution that works. Any help would be appreciated, Thanks.
This sounds like a terrible idea. If you want to include unpacked dependencies into your project, use the maven assembly plugin or the maven shade plugin.
Use the maven-dependency-plugin. Here is an example of how you can extract the content of a dependency JAR:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<destFileName>optional-new-name.jar</destFileName>
<includes>**/*.class,**/*.xml</includes>
<excludes>**/*test.class</excludes>
</artifactItem>
</artifactItems>
<includes>**/*.java</includes>
<excludes>**/*.properties</excludes>
<outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
below is the example to use of assembly pulgin and it works for me
below is the child pom plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<archive>
<manifest>
<mainClass>my parent package statup class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Parent pom plugin
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
I am using Maven to build a particular project, and within the POM I am building 3 different variants of the primary artifact using the maven shade plugin (I'm creating uber jars with various combinations of included logging frameworks). The shade plugin creates jars with alternative artifact IDs and their respective dependency-reduced-poms.
My challenge is now how to deploy these new artifacts to my remote repositories. I'm using the maven install plugin to install them to my local repo, but the maven deploy plugin requires explicit configuration of a repository URL. What I want to happen is for the plugin to adopt whichever remote repo the default deploy uses, whether its the snapshot or release repo or another repo URL that I pass in via command-line. I was hoping to find some maven property like ${project.remoterepo.url} that equated to the resolved repo. It seems silly to have to explicitly configure the remote URL when the deploy goal already does this.
Any advice appreciated. Thanks!
This is what I did to automatically select either the SNAPSHOT or RELEASE repro based on the version pattern: (I know that this is a kind of code smell but as far as the ASF is not willing to include your code for what ever reason I could solve my requirement)
<properties>
<deploy.repositoryUrl>.. url release repo ..</deploy.repositoryUrl>
<deploy.repositoryId>.. id release repo ..</deploy.repositoryId>
<deploy.repositorySnapshotUrl>.. snapshot repo ..</deploy.repositorySnapshotUrl>
<deploy.repositorySnapshotId>.. id snapshot repo ..</deploy.repositorySnapshotId>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<!-- sets the isSnapshot property to true if SNAPSHOT was used -->
<id>build-helper-regex-is-snapshot-used</id>
<phase>validate</phase>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>isSnapshot</name>
<value>${project.version}</value>
<regex>.*-SNAPSHOT</regex>
<replacement>true</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
<!-- set the properties deploy.Url and deploy.Id during validation to
either the snapshot repository or the release repository
depending on the version pattern.-->
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source><![CDATA[
pom.properties['deploy.Url']=pom.properties['isSnapshot'].equals('true') ? pom.properties['deploy.repositorySnapshotUrl'] : pom.properties['deploy.repositoryUrl'];
pom.properties['deploy.Id']=pom.properties['isSnapshot'].equals('true') ? pom.properties['deploy.repositorySnapshotId'] : pom.properties['deploy.repositoryId'];
]]></source>
</configuration>
</execution>
</executions>
</plugin>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>DeployToArtifactory</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<url>${deploy.Url}</url>
<repositoryId>${deploy.Id}</repositoryId>
<file>target/${project.build.finalName}.${project.packaging}</file>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<classifier>resources</classifier>
<pomFile>${project.build.directory}/pom/pom.xml</pomFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Tiemo Vorschütz method is a good idea, but may not work for me.
It case some 'Exception in thread "main" BUG! exception in phase 'conversion' in source unit 'script' errors.
I have changed the gmaven plugin to a newer version and fixed errors, change it from 'gmaven-plugin' 1.x to 'groovy-maven-plugin' 2.x
like this:
<properties>
<deploy.repositoryUrl>.. url release repo ..</deploy.repositoryUrl>
<deploy.repositoryId>.. id release repo ..</deploy.repositoryId>
<deploy.repositorySnapshotUrl>.. snapshot repo ..</deploy.repositorySnapshotUrl>
<deploy.repositorySnapshotId>.. id snapshot repo ..</deploy.repositorySnapshotId>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<!-- sets the isSnapshot property to true if SNAPSHOT was used -->
<id>build-helper-regex-is-snapshot-used</id>
<phase>validate</phase>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>isSnapshot</name>
<value>${project.version}</value>
<regex>.*-SNAPSHOT</regex>
<replacement>true</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
<!-- set the properties deploy.Url and deploy.Id during validation to
either the snapshot repository or the release repository
depending on the version pattern.-->
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source><![CDATA[
project.getProperties().put('deploy.Url',properties['isSnapshot'].equals('true') ? properties['deploy.repositorySnapshotUrl'] : properties['deploy.repositoryUrl']);
project.getProperties().put('deploy.Id',properties['isSnapshot'].equals('true') ? properties['deploy.repositorySnapshotId'] : properties['deploy.repositoryId']);
]]></source>
</configuration>
</execution>
</executions>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>DeployToArtifactory</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<url>${deploy.Url}</url>
<repositoryId>${deploy.Id}</repositoryId>
<file>target/${project.build.finalName}.${project.packaging}</file>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<classifier>resources</classifier>
<pomFile>${project.build.directory}/pom/pom.xml</pomFile>
</configuration>
</execution>
</executions>
</plugin>
A simple way to do this is leveraging distributionManagement and build-helper-maven-plugin as pointed our by Tiemo Vorschütz's answer.
...
<distributionManagement>
<repository>
<id>my-release</id>
<name>my-release</name>
<url>https://example.com/release</url>
</repository>
<snapshotRepository>
<id>my-snapshot</id>
<name>my-snapshot</name>
<url>https://example.com/snapshot</url>
</snapshotRepository>
</distributionManagement>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<!-- sets the repoUrl property to the correct repository depending on the type of version -->
<id>build-deploy-url</id>
<phase>validate</phase>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>repoUrl</name>
<value>${project.version}</value>
<regex>.*-SNAPSHOT</regex>
<replacement>${project.distributionManagement.snapshotRepository.url}</replacement>
<failIfNoMatch>${project.distributionManagement.repository.url}</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>deploy-file</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<file>${project.build.directory}/<!--your-file--></file>
<url>${repoUrl}</url>
<repositoryId><!--repo as per settings.xml if credentials are the same--></repositoryId>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging><!--your packaging--></packaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
I'm trying to build a Maven parent project script which incorporates a 3rd party project (with all its dependencies) and another couple of projects of mine.
I'd like to copy all dependencies (I mean ALL dependencies, including jars requested by other dependencies which in turn are requested by one of my child project) to a parent project directory, e.g. "lib", so that I can just run java with a wildcard classpath:
java -cp "lib/*" package.blah.blah.Main
I tried with various methods using the maven-dependency-plugin such as copy-dependencies, but all I can point Maven to is my own child projects, not their dependencies (I mean in the parent project pom).
This is the parent project pom I've been writing:
<?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/maven-v4_0_0.xsd">
<properties>
<server.version>7.0.2</server.version>
<ext.version>1.0-alpha3-SNAPSHOT</ext.version>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>org.kontalk</groupId>
<artifactId>tigase-kontalk</artifactId>
<version>devel</version>
<packaging>pom</packaging>
<modules>
<module>../tigase-server/modules/master</module>
<module>../tigase-extension</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-artifact</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.kontalk</groupId>
<artifactId>tigase-server</artifactId>
<version>${server.version}</version>
<type>jar</type>
</artifactItem>
<artifactItem>
<groupId>org.kontalk</groupId>
<artifactId>tigase-extension</artifactId>
<version>${ext.version}</version>
<type>jar</type>
</artifactItem>
</artifactItems>
<outputDirectory>bin</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.kontalk</groupId>
<artifactId>tigase-server</artifactId>
<version>${server.version}</version>
</dependency>
<dependency>
<groupId>org.kontalk</groupId>
<artifactId>tigase-extension</artifactId>
<version>${ext.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
It will copy just the two direct child project jars in the bin directory, but nothing else.
I'm thinking of doing a manual copy (as in a Maven copy) to brutally copy all jars from my child projects' target/dependency directories, but it just seems... brutal.
If it can't be done with the existing Maven software, I can even be content with using maven exec directly (which, I hope, should set up classpath automatically, right?)
EDIT: I'd like to modify the child projects poms as little as possible, especially the 3rd party one.
If you want to build a simply runnable jar file with maven then you can use this config:
<profiles>
<profile>
<id>dist</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>yourJarName</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>your.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>dist</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
then you can build it using mvn clean package -Pdist
edit: then you can run it with java -jar target/yourJarName.jar
I ended up using copy-dependencies. This is the main project pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/jars</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>jars</outputDirectory>
<resources>
<resource>
<directory>../tigase-extension/target/lib</directory>
<excludes>
<exclude>junit*</exclude>
</excludes>
</resource>
<resource>
<directory>../tigase-server/target/lib</directory>
<excludes>
<exclude>junit*</exclude>
</excludes>
</resource>
<resource>
<directory>../tigase-extension/target</directory>
<includes>
<include>tigase-extension-*.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
In the submodules I've added to the same plugin this code:
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
Basically, the submodules copy their dependency jars into target/lib, then the main project copies those jars into its own jars folder.
It has some project-specific filenames and other hard-coded things, but still I think this is the best option if I want to use only Maven-provided tools. Probably a new Maven plugin would have been optimal, but I don't really have the time.
I've been using maven-ear-plugin to create ear bundle and generate application.xml with custom contextRoot (everything works fine). Now I want to create 2 ear bundles and deploy them under different context path, so I defined plugin with 2 executions. But for some reason, maven-ear-plugin ignores contextRoot property and in generated application.xml it is using artifactId instead of contextRoot in both ears (so they have same context-root "app-ui").
Here is my maven-ear-plugin definition:
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>app1</id>
<phase>package</phase>
<goals>
<goal>ear</goal>
</goals>
<configuration>
<finalName>app1</finalName>
<modules>
<webModule>
<groupId>com.x.y</groupId>
<artifactId>app-ui</artifactId>
<contextRoot>/app1-ui</contextRoot>
</webModule>
</modules>
</configuration>
</execution>
<execution>
<id>app2</id>
<phase>package</phase>
<goals>
<goal>ear</goal>
</goals>
<configuration>
<finalName>app2</finalName>
<modules>
<webModule>
<groupId>com.x.y</groupId>
<artifactId>app-ui</artifactId>
<contextRoot>/app2-ui</contextRoot>
</webModule>
</modules>
</configuration>
</execution>
Any suggestions please?
The problem is that you generate the application.xml only once while you need two application.xml. For this you need to put also two executions of the generate-application-xml goal binded on the generate-resources phase of the maven lifecycle in order to generate the application.xml file two times (in two different folders preferably ^^) with two different configurations like this :
<!-- first execution for the first application.xml in folder target/app1/META-INF -->
<execution>
<id>appxml-app1</id>
<phase>generate-resources</phase>
<goals>
<goal>generate-application-xml</goal>
</goals>
<configuration>
<generatedDescriptorLocation>target/app1/META-INF</generatedDescriptorLocation>
<modules>
<webModule>
<groupId>com.x.y</groupId>
<artifactId>app-ui</artifactId>
<contextRoot>/app1-ui</contextRoot>
</webModule>
</modules>
</configuration>
</execution>
<!-- first execution for the generation of the ear with the application.xml in folder target/app1 -->
<execution>
<id>app1</id>
<phase>package</phase>
<goals>
<goal>ear</goal>
</goals>
<configuration>
<workDirectory>target/app1</workDirectory>
<finalName>app1</finalName>
<modules>
<webModule>
<groupId>com.x.y</groupId>
<artifactId>app-ui</artifactId>
</webModule>
</modules>
</configuration>
</execution>
<!-- second execution for the second application.xml in folder target/app2/META-INF -->
<execution>
<id>appxml-app2</id>
<phase>generate-resources</phase>
<goals>
<goal>generate-application-xml</goal>
</goals>
<configuration>
<generatedDescriptorLocation>target/app2/META-INF</generatedDescriptorLocation>
<modules>
<webModule>
<groupId>com.x.y</groupId>
<artifactId>app-ui</artifactId>
<contextRoot>/app2-ui</contextRoot>
</webModule>
</modules>
</configuration>
</execution>
<!-- second execution for the generation of the ear with the application.xml in folder target/app2 -->
<execution>
<id>app2</id>
<phase>package</phase>
<goals>
<goal>ear</goal>
</goals>
<configuration>
<workDirectory>target/app2</workDirectory>
<finalName>app2</finalName>
<modules>
<webModule>
<groupId>com.x.y</groupId>
<artifactId>app-ui</artifactId>
</webModule>
</modules>
</configuration>
</execution>
To improve this, you can use variables to ensure that the folders will be the same for the two executions that concerns app1, etc, this is just a draft ;)
I'm new to Maven and I'm trying to configure Maven to generate 2 jars: one for development and one for production. The only difference between them is a config.properties file that have the database connection different so I thought I could use Maven profiles.
To my surprise I can't generate both files at once. When using profiles, each time you build you have to select the profile and a jar (in my case) will be created using the profile. The thing is that it will create 2 exactly equals jars, one without a classifier and one with the classifier (like myjar.jar and myjar-prod.jar) so if I want to generate the dev and the prod jar I have to create 4 jars (running first Maven with one profile and after that with another profile)
Why is this? Doesn't make any sense to me... but ok...
My question is:
Is there a way I could avoid the two jars from being generated? I mean, I want to have different profiles, and I have accepted (with grief) to execute multiple times the build process (one for each profile), could I avoid to have each time 2 jars and have only one without the classifier?
This is my pom.xml:
<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>com.p2p.</groupId>
<artifactId>LoadACHFiles</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyProject</name>
<url>http://maven.apache.org</url>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config-*.properties</exclude>
</excludes>
</resource>
</resources>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.0</version>
<type>jar</type>
</dependency>
</dependencies>
<profiles>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--<delete file="${project.build.outputDirectory}/config.properties"/>-->
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
If you are okay with having classified jars, you may do what you want without profiles, so you may have jars for all environments with a single build command. The key is to understand how Maven filtering works.
This is expanding on an answer I provided to a similar question. Start with that setup. Then:
Create config.properties in your src/main/resources, containing properties your app needs.
my.database.url=${database.url}
my.database.user=${database.user}
my.database.pw=${database.pw}
Now, create prod.properties and dev.properties in ${basedir}/src/main/filters holding appropriate values for each environment.
database.url=URL-for-dev
database.user=user-for-dev
database.pw=pw-for-dev
When you run mvn clean package, Maven will copy the contents of /src/main/resources, including config.properties, doing property replacement during the copy. Because there are multiple executions of both resources and jar plugins, Maven will create separate classified jar files. Each will contain a config.properties file, holding the correct properties for the environment. The filters will not end up in the built jars.
I made it removing the maven jar plugin in the profile section. Changed this:
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--<delete file="${project.build.outputDirectory}/config.properties"/>-->
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
For this:
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--<delete file="${project.build.outputDirectory}/config.properties"/>-->
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>