Different dependencies for different build profiles
To quote the Maven documentation on this:
A profile element contains both an optional activation (a profile trigger) and the set of changes to be made to the POM if that profile has been activated. For example, a project built for a test environment may point to a different database than that of the final deployment. Or dependencies may be pulled from different repositories based upon the JDK version used.
(Emphasis is mine)
Just put the dependency for the release
profile inside the profile declaration itself and do the same for debug
.
<profiles>
<profile>
<id>debug</id>
…
<dependencies>
<dependency>…</dependency>
</dependencies>
…
</profile>
<profile>
<id>release</id>
…
<dependencies>
<dependency>…</dependency>
</dependencies>
…
</profile>
</profiles>
Maven Builds with Different Dependency Configurations
What I would do is have a pom.xml per client and have the dependencies in there.
If you want to try a modular way including jenkins, the solution I see is:
- make maven profiles for instance 3 profiles ABC, BCD, BD. to act as parameters
- make a jenkins job that would take a string parameter like ABC
- build the command mvn install -PABC
A more modular solution:
- use gradle, that means: convert your pom to a gradle buid file
- optionally configure what dependencies are included by default based on a groovy file you include using "apply" in your build file
- use gradle parameters to include the dependencies and override the default ones
- make a jenkins job that would take a string of parameter like dep1,dep2,dep3
- split this string into an array and iterate to build the command gradle -Pdep1 -Pdep2 -Pdep3
Maven Profile in different dependencies
Your problem does not match what should happen in practice. Your profile definition sounds about right to me:
mvn clean install
will enable the db-localhost-mysql
(as it is marked as to be activated by default) and it will add mysql-connector-java
. The same will happen if you run mvn clean install -Pdb-localhost-mysql
mvn clean install -Pdb-localhost-oracle
will add the ojdbc6
driver. The mysql profile will not be enabled (as it is triggered only if no profile is explicitly active).
That does not mean your current dependency hierarchy hasn't already one of those jars. It might come as a transitive dependency. To isolate this case and know which project needs to be fixed run mvn dependency:tree -Pdb-localhost-oracle
to look at your dependencies hierarchy when the mysql profile is not enabled.
Maven pom.xml argument that will install different dependencies depending on the value
You add something like
<profiles>
<profile>
<id>gpu</id>
<activation>
<property>
<name>gpuCuda</name>
<value>True</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-cuda-10.1</artifactId>
<version>1.0.0-beta4</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>cpu</id>
<activation>
<property>
<name>gpuCuda</name>
<value>False</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native</artifactId>
<version>1.0.0-beta4</version>
</dependency>
</dependencies>
</profile>
</profiles
Then you activate/deactivate the profiles by the command line properties you stated, like mvn -DgpuCuda=True install
.
Differentiate Dependency based on profile in Gradle file
You can proceed differently by having build_[profile].gradle
for each profile, where [profile] is the profile you pass as argument when you launch your application example ( via -P ) :
./gradlew -Pprod bootRun
So suppose you have 2 environment prod and local, in your build.gradle
you will have :
def currentProfile;
if (project.hasProperty('prod')) {
currentProfile = 'production';
apply from: rootProject.file('gradle/build_prod.gradle');
} else if (project.hasProperty('local')) {
currentProfile = 'local';
apply from: rootProject.file('gradle/build_local.gradle');
} else {
currentProfile = 'default profile';
apply from: rootProject.file('gradle/build_default.gradle');
}
println 'Current profile: "' + currentProfile + '"
You should also have 2 gradle files; build_prod.gradle
and build_local.gradle
, and there you could have different dependencies and configuration as you want.
Change maven dependency's version by using different maven profiles
Yes, this can be done (put activeByDefault
to whichever profile you need to be default).
<dependency>
<artifactId>projectB</artifactId>
<groupId>blabla</groupId>
<version>${dependency.version}</version>
</dependency>
...
<profiles>
<profile>
<id>first</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<dependency.version>1.2.3</dependency.version>
</properties>
</profile>
<profile>
<id>second</id>
<properties>
<dependency.version>2.3.4</dependency.version>
</properties>
</profile>
</profiles>
Related Topics
Picking a Random Element from a Set
Execute Another Jar in a Java Program
Lambdas: Local Variables Need Final, Instance Variables Don'T
Java: (String[])List.Toarray() Gives Classcastexception
Why Java Inner Classes Require "Final" Outer Instance Variables
Sockets: Discover Port Availability Using Java
How to Delete Stuff Printed to Console by System.Out.Println()
Createprocess Error=206, the Filename or Extension Is Too Long When Running Main() Method
Java 1.6 - Determine Symbolic Links
How to Instantiate an Inner Class with Reflection in Java
Good Way to Encapsulate Integer.Parseint()
Content Is Not Allowed in Prolog Saxparserexception
Cannot Create an Array of Linkedlists in Java
Android Changing Floating Action Button Color
Android "Gps Requires Access_Fine_Location" Error, Even Though My Manifest File Contains This
Unexpected Top-Level Exception: Com.Android.Dex.Dexexception: Multiple Dex Files Define