Cannot import when using Eclipse and Gradle - java

I am working on a java project which uses Gradle to build. I wanted to use the Eclipse WindowBuilder to help with the GUI work, so I used the Gradle Eclipse plugin, generated the Eclipse files, and imported it into Eclipse.
The problem is that none of my imports are resolving. The project builds fine using Gradle, but Eclipse can't import anything for my project. How do I solve this?
I don't know which files to include to help debug this, so if something might help let me know and I will include it.
Thanks in advance for your help!
EDIT: Here is my build.gradle file:
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
buildscript {
def rosMavenPath = "$System.env.ROS_MAVEN_PATH".split(':').collect { 'file://' + it }
def rosMavenRepository = "$System.env.ROS_MAVEN_REPOSITORY"
repositories {
rosMavenPath.each { p ->
maven {
url p
}
}
mavenLocal()
maven {
url rosMavenRepository
}
}
dependencies {
classpath group: 'org.ros.rosjava_bootstrap', name: 'gradle_plugins', version: '[0.1,0.2)'
}
}
apply plugin: 'catkin'
apply plugin: 'eclipse'
allprojects {
/*
A github url provides a good standard unique name for your project
Example below, but you may wish to switch to your own unique url.
*/
group 'com.github.rosjava.gui_test'
version = project.catkin.pkg.version
}
subprojects {
/*
See https://github.com/rosjava/rosjava_bootstrap (look for gradle_plugins)
to see what is going on under the hood.
*/
apply plugin: 'ros-java'
}

Related

Using protobuf with Gradle (IntelliJ)

I am trying to generate code for a simple protobuf example using the build instructions given here. I have been trying for awhile but I am not able to see any auto generated code in my source root.
The following is my build.gradle file
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.google.protobuf'
group = 'io.ai.vivid'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.228'
}
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.5.1-1"
}
generateProtoTasks.generatedFilesBaseDir = 'generated-sources'
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.14.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
dependencies {
compile 'io.grpc:grpc-netty-shaded:1.14.0'
compile 'io.grpc:grpc-protobuf:1.14.0'
compile 'io.grpc:grpc-stub:1.14.0'
}
Also in my build.gradle file IntelliJ complains that it cannot resolve name protobuf
Things I have tried
Sync gradle tool in IntelliJ. This is the most prominent solution
given in SO
Setting Build tools -> Gradle -> Runner -> Delelgate IDE build/run
actions on gradle to true
Clean rebuilding of the gradle project.
From my understanding of the GitHub post, when you use the protobuf plugin, the stub will be automatically generated for you. What am I missing?
You've applied idea plugin, but you didn't configure it. You need to tell idea plugin where to include the generated source code.
protobuf {
generatedFilesBaseDir = "$projectDir/src/generated"
}
idea {
module {
sourceDirs += file("${projectDir}/src/generated/main/java");
sourceDirs += file("${projectDir}/src/generated/main/grpc");
}
}
You can take a look
at a full example of a buildfile here: build.gradle
In case anyone else ends up here problems getting IntelliJ to recognise the generated sources (Red highlight imports , classes etc). Beware of the intellisense file size limit. If your generated protobuf code exceeds the default setitng of 2500KB then the file is ignored.
Got Help -> Edit custom properties and add an entry appropriate for your case e.g.
idea.max.intellisense.filesize=4000
Spent half a day faffing over different source set source folder, generated sources, and include / exclude directories. Turned out I just need to increase this value
Alternatively, you can use sourceSets:
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
}
}
}

How to setup ANTLR in eclipse with gradle?

I'm reading this article about setting up and using ANTLR in eclipse but I'm having trouble understanding the next sentence (in "4.1 Java Setup Using Gradle"):
I use a Gradle plugin to invoke ANTLR and I also use the IDEA plugin
to generate the configuration for IntelliJ IDEA.
I've downloaded what I believe to be the eclipse equivilant of the plugin the writer is referring to and I've setup the project to use gradle to build.
I've added the following code to build.gradle:
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'someJar'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
jar {
manifest {
attributes 'Main-Class': 'com.triemond.communicate.startup.Mainclass'
}
}
dependencies {
compile 'log4j:log4j:1.2.16'
}
However I don't understand what I need to do in order to follow the article. Could someone please explain me what the article is talking about in section 4.1?

How do I set up APT for Immutables such that Intellij Idea will recognize the generated code?

I'm looking into migrating from maven to gradle, in this case, gradle itself seems to be working fine, but Idea isn't recognizing the source code that Immutables is generating.
I've read this blog post on APT, it's how I got this for.
/*
* This build file was generated by the Gradle 'init' task.
*
* This generated file contains a commented-out sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/4.3/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Java
apply plugin: 'java-library'
apply plugin: 'idea'
buildscript {
repositories {
maven {
url 'https://d3vfm0n2cffdwd.cloudfront.net'
}
jcenter()
}
dependencies {
classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE'
}
}
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom 'com.xenoterracide:platform:0.1.39-SNAPSHOT'
}
}
repositories {
maven {
url 'https://d3vfm0n2cffdwd.cloudfront.net'
}
jcenter()
}
configurations {
apt
aptCompile
}
// In this section you declare the dependencies for your production and test code
dependencies {
implementation 'com.google.guava:guava'
aptCompile 'org.immutables:value'
compileOnly 'org.immutables:value'
apt 'org.immutables:builder'
// The production code uses the SLF4J logging API at compile time
implementation 'org.slf4j:slf4j-api'
// 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.
testImplementation 'junit:junit'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.mockito:mockito-core'
testImplementation 'org.hamcrest:hamcrest-library'
}
compileJava {
options.annotationProcessorPath = configurations.aptCompile
}
for more code see bitbucket
one of the things that's getting me, is that depending on what I've tried the generated java either ends up in an out directory, or build on the classpath, of course so far neither of these solves the problem.
how do I fix this so that Idea can see the source for the generated types (so that it's not all highlighted in red)?
#CrazyCoder's links helped me get closer to a solution, this seems to resolve it though.
idea {
module {
sourceDirs += file("out/production/classes/generated")
}
}

clone git repository on my gradle using Grgit

I have a gradle project which runs a script, and somewhere in it, I need to clone a git repository.
I had it running before with svn, but I change our company SCM to gitlab, and I need to change the code so it'll now clone the repo from git.
I need something that will work similar to this SVN code:
task exportLibs(type: SvnExport) {
svnUrl = "http://<svn-url>"
targetDir = "<target-dir-to-download-files>"
}
So I read about Grgit, but there was not a single example online, how to do a simple git clone (only this link http://ajoberstar.org/grgit/docs/groovydoc/org/ajoberstar/grgit/operation/CloneOp.html). If there is someone who can help me walkthrough this problem or maybe produced me to his grgit project so i will learn from it, it'll be awesome!
--Edit--
when i tried to use the grgit as below:
group 'test'
version '1.0-SNAPSHOT'
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.ajoberstar:gradle-git:1.7.2"
}
}
apply plugin: 'java'
apply plugin: 'org.ajoberstar.grgit'
org.ajoberstar.grgit.auth.hardcoded.allow=true
task pullFromGit{
doLast {
//grgit.pull()
}
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
i've use this properties to initial it, and i got the following error:
A problem occurred evaluating root project 'grgit'.
Could not get unknown property 'org' for root project 'grgit' of type org.gradle.api.Project.
There is a link on the github page of the project to some examples and the API documentation. The following snippet would solve your problem (in this case, it will clone the grgit project to the grgit directory)
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.ajoberstar:grgit:1.7.2'
}
}
task hello {
doLast {
org.ajoberstar.grgit.Grgit.clone(dir: 'grgit', uri: 'https://github.com/ajoberstar/grgit.git')
}
}
Answer to the edited question
The documentation states that org.ajoberstar.grgit.auth.hardcoded.allow is a system property. Your assignment is not a valid way to set system properties, see the answer to this question for examples on setting system properties in groovy.
my code probably had some special imports in it, cause a the end the only clone that i could have done is shell exec.
the problem is solved, but not the bug i had...

Trouble adding PDFBox to an Android application with Gradle?

I'm working on a project that assembles and builds with Gradle (though I've been using eclipse to write the code), and have been having trouble using Apache's PDFBox. I have set the classpath to the PDFBox .jar, typing echo %CLASSPATH% in cmd returns:
C:\Users\MY_NAME\.m2\repository\org\apache\pdfbox\pdfbox\1.8.6\pdfbox-1.8.6.jar
I was doing research on this earlier and someone said that you need to start the classpath with .;, which had no effect on any of my attempts to fix my problems. Anyways, when I try to compile the program with the gradlew.bat wrapper, I get these errors:
error: package org.apache.pdfbox.pdmodel does not exist
error: package org.apache.pdfbox.util does not exist
the lines these errors reference are at the very top of my .java file, simply:
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.util.*;
I tried multiple different ways of editing the build.gradle file based on both the gradle documentation and other examples of build.gradle files I have seen online. The base build.gradle file is as follows:
allprojects {
repositories {
mavenCentral()
maven {
url "https://nexus.spritzinc.com/content/repositories/PublicReleases"
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
I have tried adding a dependencies{} section in multiple places. The build.gradle file looking like this:
allprojects {
repositories {
mavenCentral()
maven {
url "https://nexus.spritzinc.com/content/repositories/PublicReleases"
}
}
}
dependencies {
compile 'org.apache.pdfbox:pdfbox:1.8.6'
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
compiled just fine, but gave me the same include errors described above. Putting the contents of dependencies{} right after repositories{} in allprojects{} gave me the following error:
Could not find method compile() for arguments [org.apache.pdfbox:pdfbox:1.8.6] on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated#7051777c.
I have also tried adding apply plugin: 'java' and apply plugin: 'eclipse' at the top of the build.gradle file, but no combination of any of these fixes work. I have copy/pasted the pdfbox-1.8.6.jar file into the project's libs folder, and that doesn't seem to help it either. Is there anyone out there who can help me include the pdfbox-1.8.6.jar file in my gradle build?
The only time I got the error you got was when I removed the "apply plugin" line. Anyway, here's my gradle script that worked:
apply plugin: 'java'
sourceCompatibility = '1.7'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
if (!hasProperty('mainClass')) {
ext.mainClass = 'org.tilman.HelloWorld'
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
compile 'org.apache.pdfbox:pdfbox:1.8.6'
}
and here's my java program:
package org.tilman;
import org.apache.pdfbox.pdmodel.PDDocument;
public class HelloWorld
{
public static void main(String[] args)
{
PDDocument doc = new PDDocument();
System.out.println("Hello world: " + doc);
}
}

Categories

Resources