Gradle sub-sub-projects with same name in does not work for eclipse - eclipse

I have the following structure
root
|- foo
| |- implementation
| | \- build.gradle
| \- interface
| \- build.gradle
|
|- bar
| |- implementation
| | \- build.gradle
| \- interface
| \- build.gradle
|
|- build.gradle
\- settings.gradle
in settings.gradle I have the following:
include ':foo:implementation', ':foo:interface'
include ':bar:implementation', ':bar:interface'
in my build.gradle on the root folder I put all of them as a dependency
dependencies {
compile project(':foo:implementation')
compile project(':foo:interface')
compile project(':bar:implementation')
compile project(':bar:interface')
}
Eclipse needs each project to have distinct names. Gradle by default uses the project name to name these in eclipse. Thus eclipse is unable to distinguish between ':foo:implementation' and ':bar:implementation:' as they both will be named 'implementation' inside Eclipse. In my mind this should have been solvable by setting the eclipse project name to something more specific, e.g.:
allprojects {
eclipse.project {
if (project == rootProject) {
name = rootProject.name
} else {
name = rootProject.name + project.path.replace(":", "-")
}
}
}
With this I get unique project names, but when I look at the .classpath xml only one of the implementation projects and one of the interface projects are present.
Any idea?

So far, I've only been working with "single dir level" multiprojects; For that scenario, there are several ways to fix it:
If you want to fix it for eclipse only and if you're using the "import gradle project" wizard, you might want to set the "Use hierarchical project names" check box. Not sure how it behaves in your case (double dir level)
Next one can hopefully help you out: how to rename the project in gradle instead of using folder name?. Probably with some extra investigation for your double dir level. It will also change the gradle project names BTW
And indeed, The eclipse plugin also provides means to change project names, haven't used it myself though...

Sadly this is a bug with Gradle; see the open GitHub issue.

One (workaround) solution is to use different groups for the subprojects:
// root/foo/build.gradle
subprojects {
group = 'org.example.foo'
}
// root/bar/build.gradle
subprojects {
group = 'org.example.bar'
}

Related

Gradle DSL - Eclipse Equivalent for IDEA Module Property

Good localtime,
I am in the process of updating legacy (4.8.1) Gradle build files for a big-McLarge-huge, multimodule project. We utilize an intellij.gradle file which has the following line (marked by comment):
idea {
module {
inheritOutputDirs = true // <-- HOW DO I DO THIS
downloadJavadoc = true
downloadSources = true
}
workspace.iws.withXml { provider ->
def node = provider.asNode()
def dynamicClasspath = node.component.find { it."#name" == "dynamic.classpath" }
if (dynamicClasspath != null) {
dynamicClasspath."#value" = "true"
}
}
From the 4.8.1 DSL docs:
If true, output directories for this module will be located below the
output directory for the project; otherwise, they will be set to the
directories specified by IdeaModule.getOutputDir() and
IdeaModule.getTestOutputDir().
Any ideas on what the Eclipse DSL equivalent of inheritOutputDirs? Should this be handled using the eclipseClasspath API? Right now everything is building fine, but the Eclipse Java builder is is flagging things.
References:
https://docs.gradle.org/4.8.1/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html
https://docs.gradle.org/4.8.1/dsl/org.gradle.plugins.ide.eclipse.model.EclipseClasspath.html
Usually this would have been picked up through sourceSets but I can't see what your project looks like...
If your subproject uses Gradle to generate sources into /build/cxf/generated-sources directory, then you can tell Eclipse via Gradle DSL to include that as a source folder like this:
plugins { id 'eclipse' }
eclipse.classpath.file.whenMerged {
// this is the brute-force approach; there is likely a better way to add a source folder
entries << new org.gradle.plugins.ide.eclipse.model.SourceFolder('build/cxf/generated-sources', null)
}
Once this is run (via gradle eclipseClasspath) you should see a build/cxf/generated-sources folder under your project node in the Package Explorer or Project Explorer. Sort of like this:
NOTE: This is untested because I don not have a sample project to work with.
There is more discussion here: How to add gradle generated source folder to Eclipse project?

Gradle Error: Project with path ''could not be found in root project ''

Check out the link for the project structure image.
RootModule
--- ChildModule1
--- ChildModule2
--- ChildModule3
ChildModule3 depends on ChildModule2 depends on ChildModule1
Each settings.gradle defines
include ':PreviousModule'
project(':PreviousModule').projectDir = new File(settingsDir, '../PreviousModule')
And build.gradle contains
implementation project(':PreviousModule')
I even tried
compile project(':PreviousModule')
but no help.
Project Structure Image
If you want to continue setting it up as a multi-module project, you would want only one settings.gradle and that would be inside the root project. Please remove others as they are unnecessary
RootModule -> settings.gradle
include "ChildModule1"
include "ChildeModule2"
//...
then in ChildModule2 -> build.gradle
dependencies {
compile project('ChildModule1')
}

Eclipse Projectdependency on multiple src directories

today I have a special problem which already took me a while at the debugger.
I have two projects Project A and Project B.
Project A has multiple src-directories.
src
├───main
│ └───java
└───generated
└───java
both are recognized by eclipse as actual src directories. Both will be compiled to bin which looks like this:
bin
├───main
└───generated
Project B has a Project-dependencie on Project A.
And now comes the strange part: When I look for a class from Project A/src/main/java via Class.forName() inside Project B it will be found. When i look for a class from Project A/src/generated/java I get a ClassNotFound exception.
I would be very glad if you could point out a way to tell eclipse to create a dependencie on both src-directories.
btw, just in case it is important: I am using java 9.
and here is an excerpt of .classpath from Project B
<classpathentry kind="src" path="/Project A"/>
Thanks for your help.
This looks like a bug of Eclipse, Gradle or a combination of both. Try to delete the run configuration and restart the application. Make also sure the gradle.build file is in sync with the Eclipse project. If all this doesn't help, you can use a single output folder as workaround:
Manually in Project > Properties: Java Build Path, in the tab Source:
Uncheck the checkbox Allow output folders for source folders
In the field Default output folder enter bin/main
or via following gradle.build snippet:
apply plugin: 'eclipse'
eclipse.classpath.file.whenMerged {
entries.find { it.path == 'src/main/java' }.output = 'bin/main'
entries.find { it.path == 'src/generated/java' }.output = 'bin/main'
}

How to to tell Eclipse/Buildship/Gradle to add dependencies to the Modulepath

I am trying to use Eclipse, Buildship, Gradle to develop java 9 applications.
Eclipse: Oxygen
Buildship: 2.2.0
Gradle: 4.3.1
I am looking for a way to tell Buildship/Gradle to add Project and External Dependencies to the Modulepath rather than the Classpath.
Here's a representation of what I see when I configure my Eclipse project Java Build Path.
Properties for TestMain
Java Build path
Source | Projects | Libraries | Order and Export
---------
Modulepath
- JRE System Library [JavaSe-9]
Classpath
- Project and External Dependencies
- Access rules: No rules defined
- External annotations: (None)
- Native library location: (None)
- coreutil-9.4.1.jar
- slf4j-api-1.7.2.1.jar
- ...
When I try to reference the automatic module coreutil in module-info.java I get the error coreutil cannot be resolved to a module.
If I manually add coreutil-9.4.1.jar to the Modulepath then the coreutil module becomes visible. This is a problem, however, since it is impractical to manually add over 60 libraries in some cases. Moreover, each time I Refresh Gradle Project they are all removed from the Modulepath.
Thanks for help.
Gaëtan
After talking to Donát Csikós at gradle (thanks Donát) adding the following to the build.gradle file solves the problem:
apply plugin: 'eclipse'
eclipse.classpath.file {
whenMerged {
entries.findAll { isModule(it) }.each { it.entryAttributes['module'] = 'true' }
}
}
boolean isModule(entry) {
// filter java 9 modules
entry.kind == 'lib' // Only libraries can be modules
}

How to do gradle build on nested projects

I am trying to build the java projects with gradle and my project structure something like below:
Root
|
|----Child
| |
| Child1 (build.gradle)
|
|
|----Child12
| |
| Child111
| |
| Child222(build.gradle)
|
settings.gradle
As you can see, the project structure, here I am doing the gradle eclipse build for all the projects (Child1, Child222) in a single attempt by modifying settings.gradle and including the child projects something like below:
include 'Child/Child1'
include 'Child12/Child111/Child222'
Build is fine with that.
Whereas, while importing the projects(Child1, Child222) into eclipse, I am getting the following error:
Creation Problems
Path for project must have only one segment.
Because, in the Child1 .project file
<projectDescription>
<name>Root/Child1</name>
<projectDescription>
project name appearing as Root/Child1, instead of Child1.
Is there anyway, with that I can import the projects into the eclipse?
I got it, I changed settings.gradle file like below:
include 'Child:Child1'
include 'Child12:Child111:Child222'
It's working fine for me.