Gradle and Eclipse project references - eclipse

I'm using Gradle with Eclipse and am trying to reference Project1 from Project2 (both under the same root directory), like so:
settings.gradle:
include ':Project1'
project(':Project1').projectDir = new File(settingsDir, '../Project1')
build.gradle
dependencies {
compile project(':Project1')
...}
Project1 is also referenced through the build path. But eclipse/gradle does not recompile and instead uses include the old jar classes. I've also tried to set the plugin to apply plugin: 'eclipse-wtp'.

Just out of curiosity, do you have a block like this somewhere in your build.gradle
eclipse.project {
referencedProjects 'Project1'
}
I ask because, if you don't tell Eclipse that this project references Project1, it won't be included in the build path.

Related

How can the source attachment, Javadoc location and native library location be specified for a Gradle project's local dependency, in Eclipse?

The Eclipse IDE for Java Developers (Neon) as well as the default Gradle plugin (Buildship?) are used.
A Gradle Git project was created using the Eclipse IDE. A local JAR is stored within the workspace/ProjectName/lib/nameOfJAR.jar directory. It was added to this project as a dependency, using the following build.gradle configuration.
...
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
flatDir {
dirs 'lib/'
}
}
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.21'
compile name: 'nameOfJAR'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
Then Project Explorer > Project A > Gradle > Refresh Gradle Project was used to update the Eclipse GUI, to display this new local dependency, via Project Explorer > Project A > Build Path > Configure Build Path > Libraries > Project and External Dependencies > [Name of JAR].
However, when expanding this section, source attachment, Javadoc location and native library location are shown as non modifiable. Can these be set from the Gradle configuration files?
How can these be set through Eclipse and Gradle?
You can place nameOfJar-sources.jar file next to the actual library in the same directory. Gradle will use that as a source attachment. I suppose the same would work for javadocs, that is nameOfJar-javadoc.jar would be picked up. I don't know how native libs are handled.
This is probably described somewhere in the Gradle docs, but I don't know where to find them.

Gradle: produced WAR file has two versions of JAR from child project

I have a root project that builds WAR, and two child projects that build JARs. The root project references the child project in this way:
apply plugin: 'war'
jar.enabled = false
war {
dependencies {
runtime project(':application1')
runtime project(':application2')
}
}
application2 depends on application1:
dependencies {
compile '...:application1:1.+'
}
The WAR file includes two versions of application1.jar: one from repository, another just built.
EDIT: Application2 has to depend on application1 as a JAR because that simplifies debugging in Eclipse with embedded Jetty: Eclipse automatically adds application1.jar to classpath of Jetty server launch configuration.
You have specified dependency on application1 project differently for the root project and for the application2.
For your application2 it was made as dependency on a library within some repository, but your root project depends on it as on a subproject. Gradle can't determine, that some library in the repo is the same, as subproject's artifact.
If you don't want to get 2 versions of the same lib, you have to make it dependent from the same library: either as
compile '...:application1:1.+'
or as
runtime project(':application1')
Anyway, it seems to be prefferable, to make it depending on the same subproject in both cases, rather then on some project and on the library in repo.

Gradle Eclipse not working correctly for subprojects

Gradle 2.4 and Eclipse Mars here. Here's my project directory structure:
myapp/ <-- root project
myapp-client/
src/main/groovy/*
build.gradle <-- just declares some client-specific deps
myapp-shared/
src/main/groovy/*
build.gradle <-- shared-specific deps
myapp-server/
src/main/groovy/*
build.gradle <-- server-specific deps and how to package server JAR
build.gradle
settings.gradle
Where myapp/build.gradle is:
allprojects {
apply plugin: 'groovy'
apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'codenarc'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
repositories {
// ...etc.
}
dependencies {
// ...etc.
}
task wrapper(type: Wrapper) {
gradleVersion = '2.4'
}
}
And where myapp/settings.gradle is:
include ':myapp-shared'
include ':myapp-client'
include ':myapp-server'
When I do a ./gradlew eclipse I get messages indicating that the command was successful. I then open Eclipse to import myapp as a series of individual subprojects.
Expectation based on what I'm used to seeing, historically
In previous projects that contained subprojects, I would go to Import >> Existing Projects into Workspace, where I would see this dialog:
I would then click Browse and select the project root (in this particular case, myapp). Eclipse would then auto-detect the 3 subprojects and populate them as a checked list inside the Projects component in the UI above. I would then click "Finish" and Eclipse would load the 3 subprojects into my workspace, each showing as a separate project (as it should).
What's actually happening
Instead, when I click on myapp as the root project, Eclipse just populates it as a single project, almost as if it doesn't detect that I have subprojects at all:
When I click Finish it imports a single project into my workspace, that has source folders for each of the 3 subprojects. And while that's just an annoyance, the real problem is that the classpath seems to be totally jacked up. For instance, I can add a class to myapp-shared, and then include it in a class defined inside myapp-client, and Eclipse doesn't force me to add an import statement into the client class! It's like the whole project is sharing the same package/namespace.
Can any Gradle gurus spot a defect in my Gradle setup that could be the cause of this?
This was nasty, but I figured it out. Posting the solution here in case anybody else runs into this.
Instead of a single allprojects closure, you need allprojects and subprojects closures. Not sure what is "safe" to put in either closure, but I got this working by simply declaring my repositories inside of allprojects and then putting everything else inside subprojects.
The problem was that I had previously ran gradle eclipse on the root project (before I added in the subprojects closure), and so Gradle generated the typical artifacts (e.g. .projects, .classpath, .settings/*) in the parent/root project directory. So...
Delete Gradle-Eclipse artifacts out of the root directory. Delete the project from your Eclipse workspace (but not the disk!!!). I'd even go so far as to restart Eclipse to clear out any caches if they exist.
Run gradle clean for good measure. Probably not needed.
Run gradle eclipse again.
Re-import into Eclipse, and all will be well in your kingdom once again, my friend.

How to add external jar files in gradle project inside the netbeans

I have created the Gradle web app which is running inside the NetBeans. Now I want to add some external jar files in it so how can I add the jar from NetBeans.
Modify your Gradle project to add your dependency and refresh it in NetBeans to pick up the changes. There is plenty of documentation how to do it. For example How to add local .jar file dependency to build.gradle file?
If you really need to take that .jar from a local directory,
Add next to your module gradle (Not the app gradle file):
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile name: 'gson-2.2.4'
}
ref:check here

How to import multiproject build in buildSrc dir in Gradle?

Hi in my buildSrc dir I have two separate projects, lets name them one and two.
I add build.gradle and settings.gradle file in buildSrc dir and in them I wrote:
settings.gradle
include \
"one",
"two"
But when I tried to use some of the plugins, which are provided from this projects, Gradle says that this imports can not be found. I searched for simmilar problems and found that I have to write in build.gradle file in buildSrc:
dependencies {
runtime subprojects.collect { owner.project(it.path) }
}
What does this line mean? And why I have to add it? BuildSrc adds copiled classes automatically on the classpath,or I am wrong?
Gradle puts the "buildSrc project" and it's runtime dependencies on the buildscript classpath automatically but does not automatically put subprojects of buildSrc on your buildscript classpath.
If you have a multiproject build like described above, buildSrc is the root project but your custom logic is in the subprojects. Now when gradle puts buildSrc.jar on the buildscript classpath it is more or less empty.
The snippet
dependencies {
runtime subprojects.collect { owner.project(it.path) }
}
adds all your subproject as runtime dependencies to your root buildSrc project. This should fix your problem as Gradle puts the buildSrc AND it's runtime dependencies on the buildscript classpath.