Can't compile gradle vaadin project in Intellij - java

I want to try out Vaadin 7 with java 8, gradle 2.1, and IntelliJ 13.1.5 on Mac OSX. I installed gradle with brew. Here's what I've done so far
I created a project using the gradle wizard in Intellij
I set up tomcat like this Creating a Project with Intellij IDEA vaadin tutorial.
I've added the vaadin-gradle-plugin to my build.gradle file.
I hit the gradle refresh button
I ran vaadinCreateProject in the intellij gradle plugin menu
I made sure my JAVA_HOME and GRADLE_HOME point to correct locations
Everything works correctly with gradle commands, I ran vaadinRun and navigated http://localhost://8080 and saw the hello world. But IntelliJ has all identifiers highlighted red with "cannot resolve symbol". I've tried every form of recompiling and clearing caches.
How can I get Intellij recognize the code?
My gradle.build file.
apply plugin: 'java'
apply from: 'http://plugins.jasoft.fi/vaadin.plugin'
sourceCompatibility = 1.5
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}

The Vaadin Gradle plugin injects the Vaadin dependencies at runtime which Intellij's JetGradle plugin does not pick up for some reason.
What you can do instead is use the Gradle idea plugin (apply plugin: 'idea' in your build.gradle) and run the idea task. After that import the project into Intellij and all dependencies should be on the classpath.
Whenever you add a dependency you need to run the idea task again so the classpath is updated.

According to JetBrains,
Vaadin plugin requires Gradle 3.0+
// Vaadin plugin requires Gradle 3.0+
task wrapper(type: Wrapper) {
gradleVersion = '3.0'
}
I am not sure your project will be OK with that spoon of groovy, but without it it shouldn't work.

Related

How can I create Gradle tasks correctly?

I'm trying to build tasks to allow me to specify my profile for my spring app using Gradle.
buildscript {
ext {
springBootVersion = '2.0.1.RELEASE'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
}
}
plugins {
id "com.diffplug.gradle.spotless" version "3.10.0"
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
spotless {
java {
googleJavaFormat()
licenseHeaderFile 'habicus.license.java'
}
}
group = 'com.remindful'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-jersey')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-web-services')
compile('org.springframework.boot:spring-boot-starter-websocket')
compile('org.springframework.session:spring-session-core')
compile("com.h2database:h2")
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile group: 'com.google.guava', name: 'guava', version: '11.0.2'
testCompile 'junit:junit:4.12'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.0.1.RELEASE'
}
// TODO: Add prod profile in application-properties
task prod {
run { systemProperty "spring.profiles.active", "prod" }
}
task dev {
run { systemProperty "spring.profiles.active", "dev" }
}
// To force debug on application boot, switch suspend to y
bootRun {
systemProperties System.properties
jvmArgs=["-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"]
}
tasks.bootRun.dependsOn build
bootRun.mustRunAfter dev
I have two questions:
1) Intellij highlight the tasks with yellow squiggly underline, so I'm wondering if the syntax here is wrong?
2) Do I use gradle-wrapper for something like this or just gradle? I'm trying to better understand the difference.
1) Intellij highlight the tasks with yellow squiggly underline, so I'm wondering if the syntax here is wrong?
Unfortunately, IntelliJ does not support gradle completely, for example, the ext block in the build script is not recognized correctly, so when accessing members defined throught the ext block, IntelliJ fails to resolve its definition & type.
So there's no need for panic when IntelliJ displays a yellow underline, you only need to take notice of the errors reported by the gradle command. If gradle build says OK, then everything is fine.
Meanwhile, IntelliJ is unable to resolve third-party plugins statically, therefore it is also unable to recognize tasks and Task classes added by these plugins. In this case, it will also show this yellowish underline complaining about something like cannot infer argument type.
The solution is to refresh all Gradle projects, by clicking the refresh button on the gradle panel, and if your build script is written correctly, these underlines will most probably vanish. This is because IntelliJ embedds with gradle using the Gradle Tooling API, and during the gradle sync process (fired by refresh all Gradle projects), third-party plugins got resolved and the whole project object model is built, so that IntelliJ would know that nothing is wrong with your tasks.
If gradle build fails, then the problem is with your own build script. On how to write gradle build scripts & tasks correctly, see Authoring Tasks - Gradle User Manual.
2) Do I use gradle-wrapper for something like this or just gradle? I'm trying to better understand the difference.
The Gradle wrapper is explained in the official userguide. To be brief, I would quote:
The Wrapper is a script that invokes a declared version of Gradle, downloading it beforehand if necessary.
So if you don't have a wrapper script in your project, you should execute gradle wrapper to generate one, and then commit it to your VCS. After that, wherever you previously execute gradle <task> in the command line, you can/should replace it with ./gradlew <task> (in *nix environment) or gradlew.bat <task> (in Windows).
The main difference between using the gradle command directly from the command line and the gradle wrapper script is, that the gradle wrapper script will download the gradle binary and uses it to execute gradle builds if no installed gradle binary is found, while using the the gradle command will only result in an error in such case.
Also, when someone else is using your project, he/she can simply clone the repo and run ./gradlew build (in *nix) or gradlew.bat build (in Windows) and your project will build fluently and successfully, regardless of whether he/she has previously installed a gradle distribution.

Gradle - Add JavaFX SDK to classpath

I am using gradle in Eclipse, and my gradle.build is pretty basic (adds java plugin, sets repos and not alot more) and I am building a JavaFX program. All my code compiles and run correctly with my build scripts with 0 errors.
I am just annoyed at the fact when I add the JavaFX SDK to my build path libraries, I can see my project has it listed. When I sync my project with Gradle, gradle removes this SDK from my classpath file.
What do I need to add to my build script to stop this from happening and gradle to normally inject it into my .classpath as it does with anything else I add?
Cheers,
P.S. I'm really new to gradle and groovy and this is my first 'project' working with it. Apart from this one annoyance, it's been smooth going.
Solved this issue: completely forgot classpath is todo witth eclipse and not java/gradle.
Adding:
apply plugin: 'eclipse'
eclipse {
classpath.containers 'org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER'
}
to my gradle.build file solved this issue.
I actually found a solution for this via https://groups.google.com/forum/#!topic/javafxports/Fn92C5ysC60 'android forum' while looking up how I could automate the building of eclipse.
Cheers if anyone looked into this.
As a side note, as I was confused about this first: A JavaFX project is no different from a Java project and you don't need to specify the fact you're using JavaFX to your ide to be able to execute JavaFX code. So I was confused why my IDE had a 'Start a new JavaFX project' and 'Start a new Gradle Project' but no JavaFX/Gradle project.
You don't 'need' a JavaFX plugin like my project originally had either.
To solve your problem, you need JavaFX-Gradle-plugin, it's a plugin that enable JavaFX support on Gradle project.
This is the link of plugin: https://github.com/FibreFoX/javafx-gradle-plugin, where you can find all infos and example...
All of you need is to start a new Gradle project, then add to your file build.gradle this code:
buildscript {
dependencies {
classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.8.2'
}
repositories {
mavenLocal()
mavenCentral()
}
}
apply plugin: 'java'
repositories {
mavenLocal()
mavenCentral()
}
dependencies{
// put here your project dependencies
}
apply plugin: 'javafx-gradle-plugin'
// these values are the examples and defaults
// you won't need them all
// configure javafx-gradle-plugin
jfx {
// minimal requirement for jfxJar-task
mainClass = 'YOUR.MAIN.CLASS'
// minimal requirement for jfxNative-task
vendor = 'YOUR NAME OR COMPANY'
// some optional task
jfxAppOutputDir = 'build'
jfxMainAppJarName = 'YOUR APPLICATION NAME.jar'
manifestAttributes = [
"Specification-Version": 1.0,
"Implementation-Version": 1,
"Built-By": "YOUR NAME OR COMPANY",
]
// for a full list of available settings, look the class "JavaFXGradlePluginExtension" on plugin project
}
This is the only plugin that I've found for use JavaFX with Gradle.
I'm working with Eclipse on project with same problem, and I've solved it a few days ago.
Hope this help,
BoGnY

Gradle unable to resolve Sonatype Nexus dependency in IntelliJ

I am using Gradle in a new IntelliJ project. We have an internal Sonatype Nexus repository and I have declared that in build.gradle. I also added the dependency to the build script.
group 'com.companyName'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven{
url 'http://host:port/nexus/content/repositories/releases'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'com.companyName', name:'abc', version: '2.2.7'
}
As you can tell this is a brand new project. IntelliJ will build the project but wont resolve the external dependency. When I look in the Gradle projects view in IntelliJ there is a red squiggly line under the dependency and it says Unable to resolve dependency. I know the dependency exists and I can use it in Maven projects.
I've search around without any solution, tried all different settings in the intelliJ project also. Any ideas?
From discussion comments on the original post:
There seem some caches be corrupt.
Delete the .gradle folder in your project and ~/.gradle/caches, then try to resolve the dependencies again.
I had a similar problem with IntelliJ 2017.1 connecting to Artifactory. I opened a Terminal, ran "./gradlew tasks" and, as a side-effect, the dependency was downloaded.
Issues like unable to resolve dependencies(even after checking that there are no typos) could be because of firewall settings.
Check with your security department on how to fix it.
In some organizations, they've local mirror. In gradle.properties you'll have to give that path in distributionUrl
Also in build.gradle use corresponding path in repositories.

How to update dependencies in Gradle

Can any body can tell me how can I do something like maven install -U (update dependencies) in gradle.
I have problem I have added new dependency to my build.gradle file
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-starter-security")
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
New dependency is:
compile("org.springframework.boot:spring-boot-starter-security")
And during build or project synchronize my IntelliJ (version 14) is not even trying download this new dependency (I'm using gradle version 2.5)
In maven project IntelliJ had something like download maven dependencies.
But for Gradle I don't see anything like this. This is like my project looks like
And can any body tell me why I don't see any *.jar on project list like maven does?
Using IntelliJ
In the Gradle tool window, click refresh button. Here is the screen:
Using terminal
You must add to your build.gradle this line
apply plugin: 'idea'
And next if you are adding some dependencies and you want synchronize IntelliJ, you just use command
gradle idea
If any one have problem with finding Gradle Tool Window it's in:
View | Tool Windows | Gradle

Intellij IDEA can't import Gradle project - "Could not initialize class javax.crypto.SunJCE_b"

I'm working on a Gradle project. The project downloads dependencies and runs perfectly fine when I do gradle run. Here's my build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'application'
mainClassName = 'myProject.MyMainClass'
repositories {
mavenCentral()
}
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.7'
compile 'org.jsoup:jsoup:1.7.2'
testCompile "junit:junit:4.11"
}
jar {
baseName = 'myproject-service'
version = '0.1.0'
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
Now I want to import the project in Intellij IDEA.
Import Gradle project >> Use default gradle wrapper (recommended) >> OK
However, when I hit OK it comes up with a dialogue saying:
Could not initialize class javax.crypto.SunJCE_b
Looking in the logs, it says
org.gradle.tooling.GradleConnectionException: Could not run build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-2.3-bin.zip'.
...
Caused by: java.lang.NoClassDefFoundError: Could not initialize class javax.crypto.SunJCE_b
at javax.crypto.KeyGenerator.a(DashoA13*..)
...
2015-03-18 21:31:17,751 [2354198] WARN - nal.AbstractExternalSystemTask - Could not initialize class javax.crypto.SunJCE_b
com.intellij.openapi.externalSystem.model.ExternalSystemException: Could not initialize class javax.crypto.SunJCE_b
at org.jetbrains.plugins.gradle.service.project.AbstractProjectImportErrorHandler.createUserFriendlyError(AbstractProjectImportErrorHandler.java:106)
...
I have no idea how to get this project to work. None of my imports from external repositories work inside IDEA without this (they all appear red). Any help would be appreciated. I'm on:
Intellij IDEA 13.1.6
Java EE 7 (jdk1.7.0_75.jdk)
Gradle 2.3
Mac OS X
SOLUTION:
Still have no idea what the issue is. I tried all the suggestions I could find online and everything I could think of: Install a different version of java, install a different IDEA, try using the original cryptography policy files, try using the unlimited strength policy files and more. None of these worked.
What did work, however, was selecting "Use a local gradle distribution", instead of using the default wrapper. I gave it my gradle install path (/usr/local/gradle-2.3), found by running which gradle (that will give you the path to the executable, namely /usr/local/gradle-2.3/bin/gradle, but I just took the directory part). Now I can build using gradle!!
My best guess is that the gradle plugin installed some other distribution of gradle that wasn't set up properly for my project.
I hope this is helpful to anyone else struggling with using gradle in Intellij IDEA.

Categories