Share dependency versions between Maven (pom.xml) and Gradle (build.gradle) - maven

I have projects built by Maven and Gradle. Is there a way to define dependency versions in a text file, e.g.:
.
|-- dep-versions.properties
|
|-- proj-by-gradle
| |-- build.gradle
| `-- settings.gradle
|
`-- proj-by-maven
`-- pom.xml
Is there an easy way to specify in dep-versions.properties, something like:
com.google.guava:guava = 26.0-jre
org.apache.commons:commons-pool2 = 2.5.0
And then use these versions in both the pom.xml and build.gradle?

You can create a BOM pom (Bill of materials) and use it in both builds. Support for importing maven BOM files was added to Gradle in version 4.6: https://docs.gradle.org/4.6/release-notes.html#bom-import

Related

Gradle: exclude build.gradle.kts in a subdirectory

Is there a way to tell Gradle
"Hey, there's a build.gradle.kts in the directory some/subdir, but please act like it's not there and don't try to mess with it at all"?
I have to keep a non-Android Kotlin project in an Android repository with a build.gradle.kts-file in the repository root for ... reasons ... and it keeps breaking my build due to plugin version conflicts.
The hierarchy layout is as follows:
|-- build.gradle.kts
|-- settings.gradle.kts
|-- src/
|-- android_project_a/
|-- build.gradle.kts
|-- android_project_b/
|-- build.gradle.kts
|-- kotlin_project/
|-- build.gradle.kts // This is what I need to exclude
If you are using command line to build, You can execute subdir as following
gradle build -x kotlin_project:build
But this is not a nice way to have it done, I wanted to check your build.gradle and setting.gradle, As you can change that once and for all, If you change the sub-projects and implementation of your sub-projects.
This kotlin_project should be a part of your setting.gradle as its being build automatically when you trigger root dir build.
You can exclude it if you want, But you have to remove it as well from dependencies at your build.gradle

Maven Archetype With Custom File Structure

I am trying to use Maven Archetypes to create a basic directory structure for a new project that is just a parent to several child projects. The structure inside archetype-resources looks like this:
root
|--- pom.xml
|--- subproject1
| \--- foo.xml
|
\--- subproject2
\--- bar.xml
Instead of generating this structure, I get the following error:
Template 'subproject1/foo.xml' not in directory 'src/main/java'
Is there a way to tell Maven to not assume the archetype is generating a Maven Java project?

Gradle multiproject doesn't add projects to classpath

I have some projects that depend on others. I also have a project that depends on two projects that each one depends on the same project. Something like this:
-Project A
* Project 1
** Project C
* Project 2
** Project C
And the structure of the workspace is like this:
-ProjectA
-ProjectC
-Project1
-Project2
All the projects are at the same level.
So in the settings.gradle in my Project A I have:
include ':Project1',':Project1:ProjectC',[...]
project(':Project1') = new File('../Project1')
project(':Project2') = new File('../Project2')
project(':Project1:ProjectC') = new File('../ProjectC')
project(':Project2:ProjectC') = new File('../ProjectC')
And in the build.gradle I do:
dependencies{ compile project('Project1'),project('Project2')
The problem is that it is not correctly added to the classpath. I think since both Project1 and Project2 depends on ProjectC it is overwritten somehow. Any ideas?
Thanks.
EDIT
Here is the tree of dependencies:
Root project 'ProjectA'
+--- Project ':ProjectB'
| \--- Project ':ProjectB:Project1'
| +--- Project ':ProjectB:Project1:Project2'
| \--- Project ':ProjectB:Project1:Project3'
\--- Project ':ProjectC'
\--- Project ':ProjectC:Project1'
+--- Project ':ProjectC:Project1:Project2'
\--- Project ':ProjectC:Project1:Project3'
For a workspace that looks like this:
rootFolder
|
|- build.gradle
|- settings.gradle
|
|- ProjectA
| |-build.gradle
|
|- Project1
| |-build.gradle
|
|- Project2
| |-build.gradle
|
|- ProjectC
|-build.gradle
Your settings.gradle should look like this (irrespective of dependency relationships of the sub projects):
include ':ProjectA',':Project1',':Project2',':ProjectC',
You're just telling the root project that there are 4 subprojects and where they are located. That's it.
Now the dependency relationships are handled inside each subproject's build.gradle files. For a dependency relationship that looks like this:
ProjectA
|-Project1
| |-ProjectC
|
|-Project2
|-ProjectC
ProjectA's build.gradle:
dependencies{
compile project(':Project1')
compile project(':Project2')
}
Project1's build.gradle:
dependencies{
compile project(':ProjectC')
}
Project2's build.gradle:
dependencies{
compile project(':ProjectC')
}
What I finally did is change in the classpath the paths that where wrong like this:
build.gradle
eclipse{
classpath{
file{
whenMerged { classpath ->
classpath.entries.find { entry ->
entry.kind == 'src' && entry.path.contains('ProjectC')
}.each{ entry ->
entry.path=file("/ProjectC")
}
}
}
}
}
And it works fine. I still don't know why gradle doesn't make the classpath correctly...

Gradle - build subprojects subprojects

Assume I have a Gradle project that looks like
RootProject
|-- SubProject1
| |- SubProject1A
| | `- build.gradle
| `- SubProject1B
| `- build.gradle
|-- SubProject2
| |- SubProject2A
| | `- build.gradle
| `- SubProject2B
| `- build.gradle
|- gradle.build
`- settings.gradle
Is it possible to build a subprojects all subprojects? I want to run
gradle :SubProject1:build
But it doesn't build the subprojects SubProject1A and SubProject1A
My settings.gradle looks like
include ":SubProject1:SubProject1A"
include ":SubProject1:SubProject1B"
include ":SubProject2:SubProject2A"
include ":SubProject2:SubProject2A"
How can I build a subprojects all subprojects?
I might be wrong, but I believe you also need a build.gradle in SubProject1 and SubProject2
Edit:
I found the solution.
Create a build.gradle in SubProject1 and SubProject2 and add a task dependency to SubProject1A and SubProject1B etc like this:
build.dependsOn ':SubProject1:SubProject1A:build', ':SubProject1:SubProject1B:build'
You can then run :SubProject1:build and it will also execute :SubProject1:SubProject1A:build and :SubProject1:SubProject1B:build

How to get this maven multi-project setup right (war + jars + static files)?

We have a (until now) small project, which consists of 4 eclipse projects. The main project consists of all the needed libs and an Ant script.
Now we want to convert this project(s) to Maven, while keeping the project stucture.
How it works:
Ant script compiles all projects (without main) to jar files
Ant script copies all those jar files into the lib folder of the main project
Ant script copies some static files (html, css, js, etc.) from the modules into some corresponding folders of the main project
Ant generates a war archive of the main project
The (maybe) problem with Maven:
All projects depend on main project
so I think it's not possible to use Maven's normal compiling behaviour
I read a bit about Maven's possibilities and it looks like the assembly plugin could solve the problem?! But I don't know how to get all that stuff working...
I thought of this project layout:
+ parent-pom (with packaging: pom)
- main-war (packaging: war)
- module1 (packaging: jar, depends on main-war)
- module2 (packaging: jar, depends on main-war)
- module3 (packaging: jar, depends on main-war and module1)
Now the parent-pom must have the <build> directive with the assembly plugin I think?
Please help me. Thank you :)
I would first suggest to change the structure of your project which means having the following folder layout:
+-- root (pom.xml packaging: pom)
+--- module1 (packaging: jar: depends on: module-common)
+--- module2 (packaging: jar: depends on: module-common)
+--- module3 (packaging: jar: depends on: module1, module-common)
+--- module-common (packaging: jar)
+--- module-war (packaging: war, depends on: module1, module2, module3)
The mentioned dependency to the war module does not make sense, cause usually you have a module which depends on an other but not on a war mdoule. So to solve that problem the simplest solution is to introduce a separate module-common which should take the code which is common for all modules. The war module has folders for html, static files etc. with a layout like the following:
.
|-- pom.xml
`-- src
`-- main
|-- java
| `-- com
| `-- example
| `-- projects
| `-- SampleAction.java
|-- resources
| `-- images
| `-- sampleimage.jpg
`-- webapp
|-- WEB-INF
| `-- web.xml
|-- index.html
`-- jsp
`-- websource.jsp
If you handle the things in a a structure like the above you don't need to use assembly plugin or what ever plugin to copy files etc. it will be handled by default. As a result you will get a war file which contains the things you need.

Resources