Sudden loss of dependencies in Eclipse Gradle project - java

I'm working with a Gradle project in Eclipse Oxygen with Java 8 and Gradle wrapper 4.6.
When i tried to add a new dependency compile 'com.googlecode.json-simple:json-simple:1.1.1' and refresh the project, all the "Project and External Dependencies" folder disappeared without a trace, which stops my project running from Eclipse (it can run through Gradle Run task though).
Removing the newly added dependency does not save me. Neither gradlew cleanEclipse & gradlew eclipse nor refreshing the project countless times.
I also attempted to clear the cache and forced project to rebuild when reimport, but no "Project and External Dependencies" shows up.
What should i do to bring back the old dependencies?
Here is my build.gradle
// Apply the java plugin to add support for Java
apply plugin: 'java'
// Apply the plugin to be runnable
apply plugin: 'application'
// Apply the eclipse plugin to import project on other machines
apply plugin: 'eclipse'
// JUnit 5
apply plugin: 'org.junit.platform.gradle.plugin'
// Define the entry point
mainClassName = 'meshIneBits.MeshIneBitsMain'
applicationName = 'MeshIneBits'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
sourceCompatibility = 1.8
// In this section you declare the dependencies for your production and test code
dependencies {
compile 'org.processing:core:3.3.5'
compile 'org.jogamp.jogl:jogl-all:2.3.2'
compile 'org.jogamp.gluegen:gluegen-rt:2.3.2'
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-linux-amd64"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-linux-armv6"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-linux-armv6hf"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-linux-i586"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-macosx-universal"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-windows-amd64"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-windows-i586"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-linux-amd64"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-linux-armv6"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-linux-armv6hf"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-linux-i586"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-macosx-universal"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-solaris-i586"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-windows-amd64"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-windows-i586"
testCompile('org.junit.jupiter:junit-jupiter-api:5.1.0',
'org.junit.jupiter:junit-jupiter-params:5.1.0')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.1.0',
'org.junit.platform:junit-platform-launcher:1.1.0')
}
eclipse {
project.natures 'org.eclipse.buildship.core.gradleprojectnature'
classpath {
file {
defaultOutputDir = file('bin/main')
whenMerged {
//change output folder for tests
def testsrc = entries.find { it.path == 'src/test/java' }
testsrc.output = "bin/test"
def testres = entries.find { it.path == 'src/test/resources' }
testres.output = "bin/test"
// Remove local ref for libs
entries.removeAll{ it.kind == "lib" }
}
containers 'org.eclipse.jdt.junit.JUNIT_CONTAINER/5', 'org.eclipse.buildship.core.gradleclasspathcontainer' }
}
}
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.1.0' }
}
jar {
baseName = applicationName
version = '0.2.0'
manifest { attributes 'Main-Class' : mainClassName }
}
Edit:
After switching to IntelliJ, the problem is solved (i used the same folder of project). So i think this is a problem in Eclipse.

I also had this problem where the "Project and External Dependencies" folder disappeared. It instead showed as 'org.eclipse.buildship.core.gradleclasspathcontainer'.
If you do not see that line, then it needs to be added to the .classpath file.
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer" />
The fix that worked for me was closing Eclipse, and running
gradlew cleanEclipse eclipse
then opening Eclipse and adding the Gradle nature to the project.

I also encountered this problem when I refactor the project name.
Following steps solve my problem:
add eclipse plugin to build.gradle
right click build.gradle, choose gradle then select refresh gradle project
only refresh gradle project didn't help me.

Related

Trying to include Gradle project as dependency, but include() not working

I have a Gradle project which depends on another Gradle project. My project structure is like this:
project1
build.gradle
settings.gradle
src/
project2
build.gradle
settings.gradle
src/
in project1/build.gradle I want to add project2 as a dependency:
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDirs = [ 'src' ]
}
}
}
include ':project2'
project(':project2').projectDir = new File("../project2")
dependencies {
compile project(':project2')
}
Unfortunately, I'm always getting this error message:
Error:(21, 0) Could not find method include() for arguments [:project2] on root project 'project1' of type org.gradle.api.Project.
I'm using Gradle 3.5 and I'm getting this error both on the command line (gradle build) and in IntelliJ. I found a few StackOverflow threads about the same issue (this and this), but they were not helpful.
The Gradle multi-project documentation also doesn't mention any specific requirements which I may be missing that can cause the error.
When I leave the include call out, I get the message that the project path could not be found in the root project.
I also tried moving the dependency to a subdirectory of project1, but without success.
I wonder what I'm doing wrong and why apparently not many other people are having the same problem. I'd be grateful for hints.
Note: this is not an Android project.
As pointed out in the first comment, include actually needs to go into settings.gradle and not into build.gradle. The same applies to changing the projectDir property.
Comment 3 gave also me another idea. The project can be included in settings.gradle as follows:
includeBuild '../project2'
and in project1/build.gradle I specify the dependency as
dependencies {
compile 'group:project2:version'
}
I generally like this better, since it's less code and looks cleaner. The downside, however, is that recursive composite builds aren't possible. So if project2 itself is also a composite build, this won't work.

How to export a Spring Boot project to an executable standalone file?

I have created a project in Spring Tool Suite with Spring Boot and Gradle, and I really don't know how to export to make it work.
I don't know much about gradle, just the basics to add dependencies from the maven repository. So in some articles says to apply the application plugin to do the task, but I don't know how to set up the configuration file and how to create the executable.
If anyone could write or link a step by step detailed explanation on how to do it, it would be very much appreciated.
This is my build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
springBoot {
mainClass = "com.rodamientosbulnes.objetivosventa.Application"
executable = true
}
jar {
baseName = 'objetivosventa'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework:spring-jdbc')
compile('net.sourceforge.jtds:jtds:1.3.1')
compile('org.apache.poi:poi-ooxml:3.13')
compile('com.miglayout:miglayout-swing:4.2')
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
Build file looks fine, you only need to run gradle build (or Run As -> Gradle -> Gradle build in the STS) to create the runnable jar.
More details about configuration of the gradle plugin are available on spring boot documentation site.
Gradle's application plugin doesn't make a single execitable for you, but it can create a distribution, which includes all the dependencies, jar-artifact for your project and 2 scripts to run it (one batch-file and linex executable).
The main thing you need to know, is that spring-boot plugin already provide all the task from application plugin you may need. All the task you can find here. You need distZip or installDist to package your project to the distribution. This task will create a ready project distribution under your project-folder/build folder. One more task you may find usefull is buildRun which will run you spring-boot application without package it into distribution.

IntelliJ can't run gradle project due to missing Class

I just gave IntelliJ a try, because Eclipse annoyed me again. I imported my gradle project(jetty, vaadin) and it went quite smoothly. But when I tried to run it I encountered the following error message during "make":
Error:gradle-resources-test:vaadinsharedwidgets: java.lang.NoClassDefFoundError: org/apache/tools/ant/util/ReaderInputStream
"vaadinsharedwidgets" is a module of the project. From what I understand from the error, IntelliJ doesn't find ant, but this is intended because I don't use ant. It also not part of the transitive dependencies. The same project runs in eclipse fine and also building it in gradle works without any problems.
Update: I just checked in Eclipse and somehow the ant.jar is on the classpath in Eclipse but I can't link it to any project. I wonder how it got there.
Update2: Missing version information:
IntelliJ: v14.0.1 CE (no plugins)
Gradle: 2.2 (used via wrapper)
Java 8 (1.8.0 b05)
Vaadin 7.3.4
build.gradle:
apply from: 'http://nexus/gradle/vaadin.gradle'
apply from: 'http://nexus/gradle/java8.gradle'
version = '1.1'
description = "Gemeinsame Vaadin-Widgets"
vaadin.widgetset 'net.xyz.vaadinsharedwidgets.VaadinsharedWidgetsWidgetset'
dependencies {
compile project(':ibhtheme')
compile 'com.vaadin:vaadin-server:' + vaadin.version
compile 'com.vaadin:vaadin-client:' + vaadin.version
}
jar{
// Include source in jar
from sourceSets.main.allJava
}
sourceSets.main.resources.srcDir 'src/main/webapp'
vaadin.gradle:
apply from: 'http://plugins.jasoft.fi/vaadin.plugin?version=0.9.2'
configurations {
def conf = 'vaadin-client'
def sources = project.sourceSets.main
def testSources = project.sourceSets.test
if (!project.configurations.hasProperty(conf)) {
project.configurations.create(conf)
sources.compileClasspath += project.configurations[conf]
testSources.compileClasspath += project.configurations[conf]
testSources.runtimeClasspath += project.configurations[conf]
project.configurations[conf].exclude group: 'org.eclipse.jetty'
}
}
vaadin {
version '7.3.4'
push true
}
java8.gradle:
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
group = 'net.xyz'
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
compile 'com.google.guava:guava:16.0.1'
compile 'org.springframework:spring-context:4.0.3.RELEASE'
testCompile 'org.testng:testng:6.8.7'
testCompile 'org.mockito:mockito-all:1.9.5'
testCompile 'org.easytesting:fest-assert-core:2.0M10'
testCompile 'org.springframework:spring-test:4.0.3.RELEASE'
}
Adding ant as an additional dependency to the module doesn't work.
I encountered the same error in a Java project with multiple subprojects in IntelliJ 14.
Updating to 15.0.1, refreshing the Gradle projects via Views → Tool Windows → Gradle in IntelliJ and restarting fixed the issue.
Choose File → Invalidate Caches / Restart from the menu and select Invalidate and Restart. That fixed the issue for me.
After some tinkering and try-and-error I found that the following code in the vaadin.gradle was the culprit and removed it:
configurations {
def conf = 'vaadin-client'
def sources = project.sourceSets.main
def testSources = project.sourceSets.test
if (!project.configurations.hasProperty(conf)) {
project.configurations.create(conf)
sources.compileClasspath += project.configurations[conf]
testSources.compileClasspath += project.configurations[conf]
testSources.runtimeClasspath += project.configurations[conf]
project.configurations[conf].exclude group: 'org.eclipse.jetty'
}
}
It was part of an outdated hack to use jetty9 instead of jetty8, which was used by older versions of the vaadin-gradle plugin. The current version uses 9.2.2 which seems to be fine.

Intellij Idea 13 UI Designer and automatic Gradle building

I've used the Intellij UI Designer to create forms for a project. Everything works fine when I'm building with idea as it handles compiling the forms for me, but as we recently switched to using Gradle for building it hasn't been possible to produce an executable jar file yet.
My google-fu has led me to several posts that explains that an ant script is needed to compile (eg link, link2, link3 ,and the one i ended on following: link4)
My project is a multi-module setup.
root build.gradle
subprojects {
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenCentral()
}
}
supproject build.gradle
apply plugin:'application'
mainClassName = "dk.OfferFileEditor.OfferFileEditorProgram"
configurations {
antTask
}
dependencies {
compile 'org.json:json:20140107'
compile project(":Shared:HasOffers Api")
//dependencies for java2c
antTask files('../../lib/javac2-13.1.1.jar', '../../lib/asm4-all-13.1.1-idea.jar', '../../lib/forms_rt-13.1.1.jar')
antTask group: 'org.jdom', name: 'jdom', version: '1.1'
}
task compileJava(overwrite: true, dependsOn: configurations.compile.getTaskDependencyFromProjectDependency(true, 'jar')) {
doLast {
println 'using java2c to compile'
project.sourceSets.main.output.classesDir.mkdirs()
ant.taskdef name: 'javac2', classname: 'com.intellij.ant.Javac2', classpath: configurations.antTask.asPath
ant.javac2 srcdir: project.sourceSets.main.java.srcDirs.join(':'),
classpath: project.sourceSets.main.compileClasspath.asPath,
destdir: project.sourceSets.main.output.classesDir,
source: sourceCompatibility,
target: targetCompatibility,
includeAntRuntime: false
}
}
But even though the compilation is successfull, a Nullpointer exception is thrown the first time I try to access one of the fields the UI Designer created. So something is not being compiled correctly.
I'm probably missing some setting, but after unsuccesfully pouring several hours into forums and google I still haven't found any solution.
So I made this a lot more complicated than needs be.
To make it work you need to change two things in your project.
A setting in IDEA 13.1.5
Settings -> GUI Designer -> Generate GUI into: Java source code
This makes IntelliJ IDEA add 3 methods into the bottom of your forms:
$$$setupUI$$$()
$$$setupUI$$$()
$$$getRootComponent$$$()
If they are missing try recompiling your project after you change the setting.
Add the missing classes
Intellij has a jar called forms_rt.jar, and I found mine in {IntelliJ IDEA Root}\lib. And renamed it to "forms_rt-13.1.1.jar"
This needs to be included during compile time to your project. If you are using Gradle as I did you could copy it to {project root}/lib and add a flatfile repository like so:
repositories {
mavenCentral()
flatDir dirs: "${rootDir}/lib"
}
After that you need to include it in your project gradle file:
dependencies {
compile name: 'forms_rt', version: '13.1.1'
}
After that it should be possible to build it both in IntelliJ IDEA and Gradle.
IntelliJ IDEA 2019.1
I found this issue still exists. It's at least somehow documented now:
If your build actions are delegated to Gradle, GUI Designer will not generate Java source code.
So by disabling the according setting
Build, Execution, Deployment | Build Tools | Gradle | Runner | Delegate IDE build/run actions to gradle
I was able to build and run the project successfully. Note that I didn't need any other settings or additional libraries from the answers above. I let Generate GUI into be set to Binary class files.
The forms_rt library is in mavenCentral.
http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22forms_rt%22
Once you have configured IntelliJ to update the SourceCode it is sufficient to just add the library to the dependencies in your build.gradle.
dependencies {
compile 'com.intellij:forms_rt:7.0.3'
}
Idea 2019.2
It seems like IntelliJ changed the settings UI when updating from 2019.1 to 2019.2, as the menu entry mentioned by Tom isn't there anymore.
I got it fixed by setting Build and run using: to IntelliJ Idea. I also changed Run tests using: to IntelliJ Idea to avoid problems while testing.
Both settings are located under File | Settings | Build, Execution, Deployment | Build Tools | Gradle.
I figured out an updated version of the gradle build workaround for a new project - https://github.com/edward3h/systray-mpd/blob/master/build.gradle
Probably won't use the form designer again though.
These are the relevant parts:
repositories {
mavenCentral()
maven { url "https://www.jetbrains.com/intellij-repository/releases" }
maven { url "https://jetbrains.bintray.com/intellij-third-party-dependencies" }
}
configurations {
antTask
}
dependencies {
implementation 'com.jetbrains.intellij.java:java-gui-forms-rt:203.7148.30'
antTask 'com.jetbrains.intellij.java:java-compiler-ant-tasks:203.7148.30'
}
task compileJava(type: JavaCompile, overwrite: true, dependsOn: configurations.compile.getTaskDependencyFromProjectDependency(true, 'jar')) {
doLast {
project.sourceSets.main.output.classesDirs.each { project.mkdir(it) }
ant.taskdef name: 'javac2', classname: 'com.intellij.ant.Javac2', classpath: configurations.antTask.asPath
ant.javac2 srcdir: project.sourceSets.main.java.srcDirs.join(':'),
classpath: project.sourceSets.main.compileClasspath.asPath,
destdir: project.sourceSets.main.output.classesDirs[0],
source: sourceCompatibility,
target: targetCompatibility,
includeAntRuntime: false
}
}
The dependency versions for jetbrains libraries are found via https://plugins.jetbrains.com/docs/intellij/intellij-artifacts.html?from=jetbrains.org#using-intellij-platform-module-artifacts and https://www.jetbrains.com/intellij-repository/releases/

Gradle doesn't seem to be resolving my project dependency properly

I'm pretty new to Gradle and am having an issue getting my a module that is dependant on another module to build properly.
So I have the following configuration for my modules.
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.10'
}
}
project(':web-service') {
apply plugin: 'war'
dependencies {
compile project(':core')
compile('com.sun.jersey:jersey-server:1.7')
compile('com.googlecode.json-simple:json-simple:1.1.1')
}
}
project(':core') {
dependencies {
compile('log4j:log4j:1.2.17')
}
}
If I try to build my core project everything succeeds as expected.
However, if I try to build the web-service project with the following command:
gradle :web-service:build
It appears to build the core project first as expected but then encounter build errors that indicate that classes that exist in the core module cannot be found.
What gives?
Turns out this was completely my fault. I dug deeper on the error messages that I was getting and found some package does not exist messages at the top. Turns out that my directory structure was not inline with my package names.

Categories

Resources