I am migrating from ant to maven.
I need to make the environment specific builds for dev and prod, I have seen many questions related to that but in my case I have some java files which will only goes into prod. How can I use those java fils to separate the builds.
my project structure looks like
parent
project A
| src
| pom
project B
| src
| pom
project C
| src
| pom
project D
| src
| pom
Parent-POM
Java files which are only used in Prod are the password utilities files.
Please let me know if you have any confusion.
Thanks in advance.
I agree with #khmarbaise, nevertheless a dirty approach could be to define the library name as a property and use 2 different profile to decide with library to use.
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>${my.library}</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
<properties>
<slf4j.version>1.7.25</slf4j.version>
</properties>
<profiles>
<profile>
<id>prod</id>
<properties>
<my.library>slf4j-jdk14</my.library>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<my.library>slf4j-log4j12</my.library>
</properties>
</profile>
</profiles>
mvn -P dev dependency:tree
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) # test ---
[INFO] test:test:jar:1.0-SNAPSHOT
[INFO] +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] \- org.slf4j:slf4j-log4j12:jar:1.7.25:compile
[INFO] \- log4j:log4j:jar:1.2.17:compile
[
mvn -P prod dependency:tree
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) # test ---
[INFO] test:test:jar:1.0-SNAPSHOT
[INFO] +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] \- org.slf4j:slf4j-jdk14:jar:1.7.25:compile
[
Related
I stuck on building complex maven multi module project based on Hexagonal architecture.
My maven module structure look like that
| graphql-intro (root module)
| graphql-intro.xml
+-- bootloader (module)
| +--- src/main/java/com/intro/graphql/ApplicationInitializer
| +--- bootloader.xml
|
+-- domain (module)
| +--- domain.xml
|
+-- infrastructure-adapters (module, root module for adapters)
| +--- infrastructure-adapters.xml
|
| +--- adapter-api-graphql (module)
| | +--- adapter-api-graphql.xml
|
| +--- adapter-persistence-in-memory (module)
| | +--- adapter-persistence-in-memory.xml
|
| +--- adapter-persistence-spring-data-jpa (module)
| | +--- adapter-persistence-spring-data-jpa.xml
root.xml look like that
...
<packaging>pom</packaging>
<modules>
<module>domain</module>
<module>infrastructure-adapters</module>
<module>bootloader</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<groupId>com.into.graphql</groupId>
<artifactId>graphql-into</artifactId>
<version>0.0.1-SNAPSHOT</version>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.intro.graphql.ApplicationInitializer</mainClass>
</configuration>
</plugin>
...
and this is infrastructure-adapters.xml
<parent>
<artifactId>graphql-into</artifactId>
<groupId>com.into.graphql</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>pom</packaging>
<artifactId>infrastructure-adapters</artifactId>
<name>infrastructure-adapters</name>
<description>infrastructure-adapters</description>
<modules>
<module>adapter-persistence-spring-data-jpa</module>
<module>adapter-persistence-in-memory</module>
<module>adapter-api-graphql-kickstarter</module>
</modules>
The problem is that during build persistence adapters cannot see class from domain module, and this is pom in spring data jpa persistence adapter
<parent>
<groupId>com.into.graphql</groupId>
<artifactId>infrastructure-adapters</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>adapter-persistence-spring-data-jpa</artifactId>
<name>adapter-persistence-spring-data-jpa</name>
<description>adapter-persistence-spring-data-jpa</description>
<dependencies>
...
<dependency>
<groupId>com.into.graphql</groupId>
<artifactId>domain</artifactId>
</dependency>
...
</dependencies>
The error is
[INFO] graphql-into ....................................... SUCCESS [ 1.875 s]
[INFO] domain ............................................. SUCCESS [ 2.582 s]
[INFO] infrastructure-adapters ............................ SUCCESS [ 0.092 s]
[INFO] adapter-persistence-spring-data-jpa ................ FAILURE [ 2.118 s]
[INFO] adapter-persistence-in-memory ...................... SKIPPED
[INFO] adapter-api-graphql-kickstarter .................... SKIPPED
[INFO] bootloader ......................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.152 s
[INFO] Finished at: 2020-04-30T11:17:26+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project adapter-persistence-spring-data-jpa: Compilation failure: Compilation failure:
[ERROR] path/graphql-into/infrastructure-adapters/adapter-persistence-spring-data-jpa/src/main/java/com/intro/graphql/persistance/jpa/products/ProductDaoAdapter.java:[3,33] package com.intro.grap
hql.dealers does not exist (it is package from domain module)
Has anyone know what could be a problem, can inside module depend from outside module or maybe the order of build is bad ?
Problem solved, I moved build configuration with spring-boot-maven-plugin to the bootloader pom and now build work. Unfortunatelly I dont know the reason why it solved problem.
I know the concept of super pom and inheritance. But still I see extra plugins magically coming up in my child module effective pom.
The project is very simple :
We have 2 POM -one for parent module and one for child module.
Parent module :
<?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>
<groupId>com.practice</groupId>
<artifactId>learning-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>child-module1</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>fr.jcgay.maven.plugins</groupId>
<artifactId>buildplan-maven-plugin</artifactId>
<version>1.3</version>
</plugin>
</plugins>
</build>
</project>
Child module POM :
<?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>
<groupId>com.practice</groupId>
<artifactId>learning-maven</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>child-module1</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
If I generate effective POM of child and parent module I get this :
(I am showing the effective POM data of plugins and goals through buildplan-maven-plugin for ease of reading. I have checked these plugin data with respect to effective POM too.)
[INFO] ------------------------------------------------------------------------
[INFO] Building learning-maven 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- buildplan-maven-plugin:1.3:list (default-cli) # learning-maven ---
[INFO] Build Plan for learning-maven:
----------------------------------------------------------
PLUGIN | PHASE | ID | GOAL
----------------------------------------------------------
maven-install-plugin | install | default-install | install
maven-deploy-plugin | deploy | default-deploy | deploy
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building child-module1 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- buildplan-maven-plugin:1.3:list (default-cli) # child-module1 ---
[INFO] Build Plan for child-module1:
---------------------------------------------------------------------------------------
PLUGIN | PHASE | ID | GOAL
---------------------------------------------------------------------------------------
maven-resources-plugin | process-resources | default-resources | resources
maven-compiler-plugin | compile | default-compile | compile
maven-resources-plugin | process-test-resources | default-testResources | testResources
maven-compiler-plugin | test-compile | default-testCompile | testCompile
maven-surefire-plugin | test | default-test | test
maven-jar-plugin | package | default-jar | jar
maven-install-plugin | install | default-install | install
maven-deploy-plugin | deploy | default-deploy | deploy
[INFO] ------------------------------------------------------------------------
If you see in child module , you are getting more plugins than the parent.
If you want to see the super POM , it is here.
So where does this extra plugins coming from in child module ?
I do not see it in your child pom, but you probably set the packaging to jar somewhere. The standard lifecycle of Maven defines several goals to be called depending on the packaging, and the Maven plugins you list would fit well to those goals.
mvn dependency:build-classpath -DincludeScope=test
doesn't show test jars that have been added to the pom.xml
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_${scala.binary.version}</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_${scala.binary.version}</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
However, mvn -f pom.xml dependency:tree|grep tests does show these test jars. Is this some kind of a bug in maven?
Using `mvn` from path: /usr/bin/mvn
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) # java8-tests_2.10 ---
[INFO] org.apache.spark:java8-tests_2.10:pom:1.6.0-cdh5.11.0-SNAPSHOT
[INFO] | | +- org.apache.avro:avro-ipc:jar:tests:1.7.6-cdh5.11.0-SNAPSHOT:compile
[INFO] +- org.apache.spark:spark-core_2.10:test-jar:tests:1.6.0-cdh5.11.0-SNAPSHOT:test
[INFO] +- org.apache.spark:spark-streaming_2.10:test-jar:tests:1.6.0-cdh5.11.0-SNAPSHOT:test
I am trying the above in a more complicated project mixing scala and java code. On a simpler sample project this does work.
Overall applicatiom I'm using Apache HttpComponents dependency:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
But also another library uses this artifact, but different version (4.3.2, not 4.5.2):
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
</dependency>
The problem is that API between this versions is changed and I'm getting this error:
Caused by: java.lang.ClassNotFoundException: org.apache.http.ssl.SSLContexts
How I can say to maven not to override Sendgrid's version of HttpComponents (4.3.2) with 4.5.2?
EDIT: version of httpcomponents is specified in dependencyManagement section of parent pom
Given the following parent pom.xml section:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<modules>
<module>module-a</module>
<module>module-b</module>
</modules>
Indeed in module-a the dependency tree is the following, executing:
mvn dependency:tree
We get as part of the output:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) # module-a ---
[INFO] com.sample:module-a:jar:0.0.1-SNAPSHOT
[INFO] \- com.sendgrid:sendgrid-java:jar:2.0.0:compile
[INFO] +- org.json:json:jar:20140107:compile
[INFO] +- org.apache.httpcomponents:httpcore:jar:4.3.2:compile
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.5.2:compile
[INFO] | +- commons-logging:commons-logging:jar:1.2:compile
[INFO] | \- commons-codec:commons-codec:jar:1.9:compile
[INFO] +- com.sendgrid:smtpapi-java:jar:1.0.0:compile
[INFO] \- org.apache.httpcomponents:httpmime:jar:4.3.4:compile
Note:
We get org.apache.httpcomponents:httpclient:jar:4.5.2:compile
We also get org.apache.httpcomponents:httpcore:jar:4.3.2:compile
A potential versons mismatch happens here between libraries of the same family
Adding then to the module-a's pom.xml the following:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
</dependency>
</dependencies>
</dependencyManagement>
And re-running our dependency tree execution, we get as part of the output:
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) # module-a ---
[INFO] com.sample:module-a:jar:0.0.1-SNAPSHOT
[INFO] \- com.sendgrid:sendgrid-java:jar:2.0.0:compile
[INFO] +- org.json:json:jar:20140107:compile
[INFO] +- org.apache.httpcomponents:httpcore:jar:4.3.2:compile
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.3.2:compile
[INFO] | +- commons-logging:commons-logging:jar:1.1.3:compile
[INFO] | \- commons-codec:commons-codec:jar:1.6:compile
[INFO] +- com.sendgrid:smtpapi-java:jar:1.0.0:compile
[INFO] \- org.apache.httpcomponents:httpmime:jar:4.3.4:compile
We now get httpcore and httpclient aligned, with the versions we wanted.
Also note the httpmime to version 4.3.4, it's a fix version change, but still a misalignment (should be harmless though).
In this case it seems you are adding governance at parent level in dependencyManagement (good approach), but then at the level of one of the modules you need to override it. That can happen, but better to properly comment it, for maintenance and for the future yourself looking at it in the future.
Also note: other modules in this project would not be affected by this change, that is, they will still get version 4.5.2. If the final result of the whole multimodule build is an ear or war file, for example, carefully check what you eventually get.
It is impossible in a simple maven project to have 2 different versions of the same artifact in the classpath. So you cannot have 4.3.2 and 4.5.2 versions in the classpath simultaneously.
However there are several options... You can either
use in your project the older version (4.3.*), compatible with sendgrid-java dependency (simplest way); or
update sendgrid-java dependency, if newer one is compatible with http components 4.5.* (preferred way); or
mark sendgrid-java as a 'provided' dependency, build a separate class loader in runtime and load it with correct dependencies versions (a bit tricky, but I saw this approach in a couple bank applications)
I'll explain the question with my real situation.
I use logback 1.0.1 for logging, and it includes SLF4J 1.6.4 as a dependency. I also use the SLF4J API bridges for legacy logging API's (java.util.logging, log4j and commons-logging), which are not explicit dependencies. These must also (preferrably) be version 1.6.4.
Trying to make my pom.xml as neat and error-free as possible, I'd like to enforce that these API bridges be the same version as SLF4J. The only way I know is to manually define them as dependencies in my pom.xml using version 1.6.4. If I ever update logback and the required SLF4J version is raised, I'd need to remember to change the bridge API's to the proper version.
Can I somehow hook the legacy API's version to the version of the transitive dependency SLF4J?
Current pom.xml:
<properties>
<org.slf4j.version>1.6.4</org.slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.1</version>
<!-- requires SLF4J 1.6.4 -->
</dependency>
<!-- ... -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
<!-- here, how to bind this version value to SLF4J's version? -->
<scope>runtime</scope>
</dependency>
<!-- the other two bridge API's go here -->
</dependencies>
Not in a very beautiful way :/
There is the maven enforcer plugin: http://maven.apache.org/enforcer/enforcer-rules/
so you can ban the transitive dependencies and include the version you want: http://maven.apache.org/enforcer/enforcer-rules/bannedDependencies.html
If you use a property for the good version you dont need to mess around in the enforcer plugin version.
Dependency Convergence may help you. Thank you #wemu!
I have a same? problem.
You can clone https://gist.github.com/f35db1ac6dc8b6f45393.git
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
<scope>runtime</scope> <!-- depends on slf4j-api:1.7.7 -->
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${unified.slf4j-api.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${unified.slf4j-api.version}</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<unified.slf4j-api.version>1.7.7</unified.slf4j-api.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<dependencyConvergence/>
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
At this point, things just work.
$ mvn -Dverbose=true dependency:tree verify
...
[INFO] test:enforcer-dependency-convergence-test:jar:0.1-SNAPSHOT
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.3:runtime
[INFO] | +- ch.qos.logback:logback-core:jar:1.1.3:runtime
[INFO] | \- (org.slf4j:slf4j-api:jar:1.7.7:runtime - omitted for duplicate)
[INFO] +- org.slf4j:log4j-over-slf4j:jar:1.7.7:runtime
[INFO] | \- (org.slf4j:slf4j-api:jar:1.7.7:runtime - omitted for duplicate)
[INFO] \- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (default) # enforcer-dependency-convergence-test ---
[INFO]
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...
$
Now let's change the slf4j-api's version.
$ mvn -Dunified.slf4j-api.version=1.7.13 -Dverbose=true dependency:tree verify
...
[INFO] test:enforcer-dependency-convergence-test:jar:0.1-SNAPSHOT
[INFO] test:enforcer-dependency-convergence-test:jar:0.1-SNAPSHOT
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.3:runtime
[INFO] | +- ch.qos.logback:logback-core:jar:1.1.3:runtime
[INFO] | \- (org.slf4j:slf4j-api:jar:1.7.7:runtime - omitted for conflict with 1.7.13)
[INFO] +- org.slf4j:log4j-over-slf4j:jar:1.7.13:runtime
[INFO] | \- (org.slf4j:slf4j-api:jar:1.7.13:runtime - omitted for conflict with 1.7.7)
[INFO] \- org.slf4j:slf4j-api:jar:1.7.13:compile
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (default) # enforcer-dependency-convergence-test ---
[WARNING]
Dependency convergence error for org.slf4j:slf4j-api:1.7.7 paths to dependency are:
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
+-ch.qos.logback:logback-classic:1.1.3
+-org.slf4j:slf4j-api:1.7.7
and
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
+-org.slf4j:log4j-over-slf4j:1.7.13
+-org.slf4j:slf4j-api:1.7.13
and
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
+-org.slf4j:slf4j-api:1.7.13
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.DependencyConvergence failed with message:
Failed while enforcing releasability the error(s) are [
Dependency convergence error for org.slf4j:slf4j-api:1.7.7 paths to dependency are:
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
+-ch.qos.logback:logback-classic:1.1.3
+-org.slf4j:slf4j-api:1.7.7
and
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
+-org.slf4j:log4j-over-slf4j:1.7.13
+-org.slf4j:slf4j-api:1.7.13
and
+-test:enforcer-dependency-convergence-test:0.1-SNAPSHOT
+-org.slf4j:slf4j-api:1.7.13
]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
...
$
Just don't directly depend on slf4j in your top level pom.