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/
Related
I have multi module gradle library project; no application module. I replicated this scenario by creating gradle init library project. The idea is to have convention plugin to handle cpd,pmd, spotBugs and other checks, and apply to each modules as necessary.
The project structure is as follows
Root project 'multi-city'
+--- Project ':lib'
├── build.gradle
└── src
--- Project ':location'
├── build.gradle
└── src\
I have created the buildSrc folder in the root project as well. The rootProject has settings.gradle folder with the following content
rootProject.name = 'multi-city'
include('lib')
include 'location'
buildSrc/main/src/groovy/com.dilla.city.java-convention.gradle
plugins {
id 'java'
id 'de.aaschmid.cpd'
}
repositories {
mavenCentral()
}
buildSrc/build.gradle
plugins {
id 'groovy-gradle-plugin'
}
repositories {
gradlePluginPortal()
}
dependencies {
implementation 'de.aaschmid:gradle-cpd-plugin:3.1'
}
I added the convention plugin to one of the library,that is 'location'
location/build.gradle
plugins {
id 'java'
id 'com.dilla.city.java-convention'
}
cpd {
ignoreFailures = true
}
tasks.withType(de.aaschmid.gradle.plugins.cpd.Cpd) {
reports {
xml.enabled = true
text.enabled = false
}
source = files('src/main/java')
}
Under location/src/main/java I have created two java files to see if cpd is work as expected, and run the below command.
./gradlew :location:clean :location:build
The issue is the following warning keep popping up. I have seen similar question raised on plugin forum, but the suggestions on the warning logs are not ideal for me. I want each module to use cpd individually
I get the following error
WARNING: Due to the absence of 'LifecycleBasePlugin' on project ':lib' the task ':cpdCheck' could not be added to task graph. Therefore CPD will not be executed. To prevent this, manually add a task dependency of ':cpdCheck' to a 'check' task of a subproject.
1) Directly to project ':location':
check.dependsOn(':cpdCheck')
2) Indirectly, e.g. via project ':lib':
project(':location') {
plugins.withType(LifecycleBasePlugin) { // <- just required if 'java' plugin is applied within subproject
check.dependsOn(cpdCheck)
}
}
> Task :location:cpdCheck
CPD found duplicate code. See the report at file:///home/da/Desktop/multi-city/location/build/reports/cpd/cpdCheck.xml
The first option did not work for me. Then I copy pasted the second option in ':lib' module and it did make the warning disappear. Now, if I want to run lib module like below
./gradlew :lib:clean :lib:build
I get similar warning but now on the reverse, and suggest to add the following to ':location' module
project(':lib') {
plugins.withType(LifecycleBasePlugin) { // <- just required if 'java' plugin is applied within subproject
check.dependsOn(cpdCheck)
}
}
Now the more library projects I have the more snippet I add to remove these warning, but I do not want this approach. Is there way to do it? I am building the libraries individually and not sure why it trigger cpd check on the other modules. Is there a way to turn off the warning? or Is it ideal to do so?
I have missed the information that this issue has been identified as a bug. I have just come across. I thought it was resolved issue
I'm working on a custom Gradle plugin. For some reason IntelliJ is unable to find the sources of the gradle-api artifact and only shows the decompiled .class file. I am already using the -all distribution of the Gradle Wrapper (which includes some sources, but apparently not the ones I need right here). Clicking Download... results in an error:
Sources not found: Sources for 'gradle-api-6.5.1.jar' not found
How do I correctly attach/choose sources for gradle-api in IntelliJ?
EDIT:
I have a minimal Gradle plugin with code like that (taken from the official samples):
plugins {
id 'java-gradle-plugin'
}
repositories {
jcenter()
}
dependencies {
testImplementation 'junit:junit:4.13'
}
gradlePlugin {
// ...
}
According to this excellent manual you should add gradleApi() as a runtimeOnly dependency:
dependencies {
//...
runtimeOnly(gradleApi())
I guess that, the default Intellij config use gradle from gradle-wrapper.properties file will use /gradle/wrapper/gradle-wrapper.jar, but it doesn't contain source code. what you need is a jar like gradle-wrapper-all.jar. But I don't know how to let Gradle redownload that. Just setting Wrapper.DistributionType.ALL is not working.
Solution
set Wrapper.DistributionType.ALL
wrapper {
jarFile = file(System.getProperty("user.dir") + '/gradle/wrapper/gradle-wrapper.jar')
gradleVersion = '6.7.1'
distributionType = Wrapper.DistributionType.ALL
}
I download Gradle, and use it. Set two things here and refresh it.
Here is the source code, the version is right and with all in the name (gradle-6.7.1-all):
delete gradle dir
run "gradle wrapper"
check the suffix "-all" in the file gradle/wrapper/gradle-wrapper.properties
sample:
distributionUrl=https://services.gradle.org/distributions/gradle-7.5-all.zip
Gradle project sync failed and did not get answers from other related questions. Here are the details of my situation.
Initial sync attempt yielded the following error message:
Unsupported method: BaseConfig.getApplicationIdSuffix().
The version of Gradle you connect to does not support that method.
To resolve the problem you can change/upgrade the target version of Gradle you connect to.
Alternatively, you can ignore this exception and read other information from the model.
I have Android Studio 3.0. The build.gradle file includes the following dependency:
classpath 'com.android.tools.build:gradle-experimental:0.2.1'
The gradle-wrapper.properties file includes the following distribution URL:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip
According to https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#update_gradle I need to make some changes.
First, update the Gradle version for Android Studio 3.0 in gradle-wrapper.properties:
distributionUrl=\
https://services.gradle.org/distributions/gradle-4.1-all.zip
(I believe the backslash right after the equal sign is an error and did not make that change.)
Second, add the following buildscript repository to build.gradle:
google()
Third, change the build.gradle dependency:
classpath 'com.android.tools.build:gradle:3.0.1'
The second and third changes apply the latest version of the Android plug-in.
When I try to sync after these changes it fails again with the following new error:
Plugin with id 'com.android.model.application' not found.
The error refers to the first line of build.gradle:
apply plugin: 'com.android.model.application'
What is happening and why? I recently added NDK to Android Studio. The project I'm trying to sync includes C code. I'm not completely certain I added NDK correctly. I wonder if that could be part of the problem.
First, the gradle-wrapper.properties is incorrect. You must include \ in it. It should be something like this:
#Sat Jun 17 17:47:18 CEST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
Then, add the experimental classpath to project build.gradle. Something like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle-experimental:0.7.0-alpha4"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Check the Experimental Plugin User Guide for details.
Go to your build.gradle version or update it
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
You should change you dependencies classpath
All steps are here :
Open build.gradle and change the gradle version to the recommended version:
classpath 'com.android.tools.build:gradle:1.3.0' to
classpath 'com.android.tools.build:gradle:2.3.2'
Hit 'Try Again'
In the messages box it'll say 'Fix Gradle Wrapper and re-import project' Click that, since the minimum gradle version is 3.3
A new error will popup and say The SDK Build Tools revision (23.0.1) is too low for project ':app'. Minimum required is 25.0.0 - Hit Update Build Tools version and sync project
A window may popup that says Android Gradle Plugin Update recommended, just update from there.
Now the project should be runnable now on any of your android virtual devices.
Make sure your Gradle version is compatible with your Android Gradle Plugin.
https://stackoverflow.com/questions/24795079
https://developer.android.com/studio/releases/gradle-plugin#updating-gradle
I have been trying to find the correct settings for IntelliJ's annotation processing in order for it to co-exist with Gradle's build process.
Whenever I build from IntelliJ I cannot get it to recognise the generated sources from the gradle-apt-plugin.
My requirements for my project are:
Building between IntelliJ and Gradle should be seamless and not interfere with the process of each other
I need to use IntelliJ's Create separate module per source set option
I need to use IntelliJ's folder based structure
IntelliJ needs to be able to recognise and autocomplete AutoValue classes
Here are the steps for a MCVE in order to reproduce the issue with IntelliJ 2017.2.4 and Gradle 3.5:
Create a new Gradle project from IntelliJ
Check the Create separate module per source set option
Open build.gradle file:
Add the following plugins block:
plugins {
id 'java'
id 'net.ltgt.apt' version '0.12'
}
Add the following dependencies block
dependencies {
compileOnly 'com.google.auto.value:auto-value:1.5'
apt 'com.google.auto.value:auto-value:1.5'
}
Go to Settings → Build, Execution, Deployment → Annotation Processors
Check the Enable Annotation Processing
Create a class:
#AutoValue
public abstract class GeneratedSourcesTest {
static GeneratedSourcesTest create(String field) {
return new AutoValue_GeneratedSourcesTest(field);
}
public abstract String field();
}
On IntelliJ run Build → Build Project
Open the GeneratedSourcesTest class, on the static factory method, everything compiles fine but I get the error:
cannot resolve symbol ‘AutoValue_GeneratedSourcesTest’
How can I make the AutoValue_GeneratedSourcesTest class accessible from IntelliJ?
After importing your Gradle project under IDEA do the following steps:
Set annotation processing configuration as follows:
Run menu: Build - Build Project
Right click on each new generated folder and select: Mark Directory as - Generated Sources Root so it was marked as follows:
Add /generated to project's .gitignore file
That's a minimal viable configuration which will provide full IDE support for generated classes.
The drawback is, whenever Gradle project gets re-imported the generated folders will need be marked as Generated Sources Root again.
Perhaps this can be improved with adding these paths as source sets under build.gradle.
Sometimes it happens that IDEA modules lose their compiler output path settings in result of the above. It's sufficient to just set it back to their default folders.
The answers are (should be) in the README for the gradle-apt-plugin: https://github.com/tbroyer/gradle-apt-plugin
Namely, also apply the net.ltgt.apt-idea plugin.
Btw, I recommend delegating build/run actions to Gradle in IntelliJ. Sure it's a bit slower, but requires zero setup in the IDE and works reliably. That said, it should also work OK if you don't.
Just have your build.gradle with these and it works fine, no need of touching intellij, source set etc..
plugins {
id 'java'
id "net.ltgt.apt" version "0.20"
}
apply plugin: 'idea'
apply plugin: 'net.ltgt.apt-idea'
group 'abc'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "com.google.auto.value:auto-value-annotations:1.6.2"
annotationProcessor "com.google.auto.value:auto-value:1.6.2"
}
I didn't have to do anything to intellij using maven by adding the optional true tag.
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.9</version>
<optional>true</optional>
</dependency>
I have a gradle project in Intellij Idea that contains two modules:
foo-core
|--src
| |--main
| | |--java
|--build.gradle
foo-test
|--src
| |--test
| | |--groovy
|--build.gradle
build.gradle
settings.gradle
foo-test contains a spock test that should test a class from foo-core. When I try to run the tests in foo-test from IntelliJ Idea, I get the following error:
Groovyc: unable to resolve class ...
But when I run them from the command line with gradle :foo-test:test, everything works just fine.
Here is my toplevel settings.gradle:
include ':foo-core', ':foo-test'
And here is the build.gradle from foo-test:
buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'groovy'
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile project(':foo-core')
compile "org.codehaus.groovy:groovy-all:2.2.1"
compile "com.google.inject:guice:3.0"
testCompile "org.spockframework:spock-core:0.7-groovy-2.0"
testCompile group: 'junit', name: 'junit', version: '4.11'
}
The project dependencies of foo-test are correct in Idea. When I "Refresh all Gradle Projects", any changed dependencies are imported correctly.
I have already tried cleaning all caches in Idea.
Why can I run the tests in gradle on the command line, but cannot compile them in IntelliJ Idea?
Update: Answers to questions from comments
Idea version 13
I created an Android Gradle project in Idea and added more modules by creating the directories+gradle files and adding them in the toplevel "settings.gradle". Then I did a "Refresh all Gradle Projects" in Idea. I do not use the gradle idea plugin.
The source and test directories are recognized by Idea. All project settings seem to be correctly imported in Idea (from gradle). I can use the java classes from foo-core in another java project (foo-ui), but can not use them in the groovy files.
Just compiling in IntelliJ does not work. A full rebuild does not work either. Even "Refresh all Gradle Projects" produces the Groovyc errors.
Also, the tests already worked when I had only two modules (foo-ui and foo-test), but stopped working after I moved some classes to a new module (foo-core).
Update 2: I have now updated to Idea 13.0.1. I have also tried to close the project, delete all .iml files and "build" directories and import it again. Both did not work.
This was, in fact, a bug of IntelliJ Idea: http://youtrack.jetbrains.com/issue/IDEA-117676
Everything works fine with IntelliJ Idea 13.0.2 and Gradle 1.9.