I'm working on a Scala project in IntelliJ IDEA, and using Gradle for configuration and dependency management.
Every time I add some new dependency and sync it into IDEA, it rewrites Additional compiler options into -target:jvm-1.8, what results in a error when I try to make it with IDEA:
Error:scalac: 'jvm-1.8' is not a valid choice for '-target'
Error:scalac: bad option: '-target:jvm-1.8'
So I need to fix it manually.
Can I set target JVM version or compiler parameters (need also -feature option) in my build.gradle, so it would take them from there?
Thanks!
You can add sourceCompatibility = '1.7' to your gradle build file and then refresh, the project will build normally. Note that this way you cannot have Java 8 source in your code, this is a temporary hack until Gradleware or JetBrains solve the issue.
You can also tamper with the IDEA project XML that is generated when running gradle idea. That would give you the option to include additional flags as well. You can check your current xml by opening the project .iml file and identifying what needs to be changed, then use the following code:
apply plugin: 'idea'
idea {
module {
iml {
withXml {
def node = it.asNode()
//modify xml
}
}
}
}
Would not recommend it though, as you will have to stick with using gradle idea and won't be able to properly import straight from the IDEA, but currently there is no better way to have additional flags. Documentation - idea plugin and idea module
I was searching for a solution for this for ages, finally found it - sharing for anyone else reading this.
Taken from: https://devnet.jetbrains.com/message/5523902
subprojects {
tasks.withType(ScalaCompile) {
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
}
}
Related
I'm trying to create a .jar file for my eclipse project. Through export option, I am able to create .jar, but I want to use Gradle build task to do so. Problem is that, all tasks are disabled for this particular project only, as showing in the following image...
For another project, these Gradle tasks are working fine. See its build.gradle file...
apply plugin : 'eclipse'
apply plugin: 'java-library'
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile group:"org.apache.pdfbox", name:"pdfbox", version:"2.0.6"
compile group:"org.apache.pdfbox", name:"pdfbox-tools", version:"2.0.6"
compile 'com.google.code.gson:gson:2.8.2'
compile 'org.json:json:20180130'
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:21.0'
testImplementation 'junit:junit:4.12'
}
It's settings.gradle file contains a single line that is...
rootProject.name = 'schedule-extractor'
Can anybody suggest what I am missing here?
Have you check this buildship's issue:
https://github.com/eclipse/buildship/issues/511
The following comment resolved it for me (I was having the same problem) :
https://github.com/eclipse/buildship/issues/511#issuecomment-367392031
For those who had a similar problem in include builds - Check the file
.setting/org.eclipse.buildship.core.prefs whether the
connection.project.dir property is empty. If not, make it empty. In
my case the grayed tasks become green again.
In my case, It happened when I was using composite build concept of Gradle. According to this approach, One build could be dependent on another build. In this case, the projects may behave like this.
For example, have a look on my composite build setup in settings.gradle...
rootProject.name = 'BackendRESTServices'
includeBuild ('../Algorithms')
includeBuild ('../Utilities')
Now I won't be able to build Algorithms & Utilities projects from Gradle Tasks tab as shown in the attached image of question.
I am trying to setup JUnit testing in my Gluon JavaFX Application. I am using the Gluon Eclipse Plugin with Gradle and Java 8.
My build.gradle file looks like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.0-b10'
}
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
}
dependencies {
compile 'com.gluonhq:ignite-dagger:1.0.0'
compile 'org.elasticsearch:elasticsearch:1.6.0'
compile 'ch.qos.logback:logback-classic:1.1.5'
testCompile 'junit:junit:4.12'
}
mainClassName = 'com.me.MyApplication'
jfxmobile {
android {
manifest = 'src/android/AndroidManifest.xml'
}
ios {
infoPList = file('src/ios/Default-Info.plist')
}
}
Resolving the dependency is no problem, but when running the 'test' task, gradle throws an error like this:
When running gradle with java 8, you must set the path to the old jdk, either with property retrolambda.oldJdk or environment variable JAVA6_HOME/JAVA7_HOME
Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-2.2.1-all.zip'.
I already tried to add the retrolambda plugin to gradle according to the plugin's README on GitHub, but it doesn't work so far. Could someone tell me what to do to configure my Gluon project so that I am able to run my JUnit tests with Gradle?
Some important addidtions:
For the plugin version it says: Gluon Tools 1.0.0.201508201514
I think I fogot to mention that I want to use Dagger dependency injection with Gluon Ignite which might be the real problem in my case as it requires Java 8 and might conflict with javafxports or something else. However, I'm not able to make full sense of the various error messages I've seen.
My tests are empty, but they aren't even run, because it fails before.
Your problem seems like a retroLambda configuration issue. If you go through the configuration page for the plugin, it states that if you don't have an environment variable set for JAVA6_HOME or JAVA7_HOME than you need to explicitly define oldJdk for the plugin to work properly.
Here is a Gradle build as advised here for using play web framework.
plugins {
id 'play'
id 'idea'
}
repositories {
jcenter()
maven {
name "typesafe-maven-release"
url "https://repo.typesafe.com/typesafe/maven-releases"
}
ivy {
name "typesafe-ivy-release"
url "https://repo.typesafe.com/typesafe/ivy-releases"
layout "ivy"
}
}
It works fine when building, launching etc... from command line but once the project is imported in intellij (idea's project files generated with gradle idea), dependencies (from the play plugin) don't show up in the project view/external libraries (even after having hit "refresh all gradle projects" in the gradle panel).
Thanks :)
PS: intellij 15.0.2 / gradle 2.6 / play plugin
Answer found here. Apparently the gradle idea plugin needs to be told explicitely how to wire the dependencies.
To sum up :
Create a typical play layout
Add a build.gradle (as below)
Type gradle idea to generate idea's project files
Open the project in intellij
plugins {
id 'play'
id 'idea'
}
repositories {
jcenter()
maven {
name "typesafe-maven-release"
url "https://repo.typesafe.com/typesafe/maven-releases"
}
ivy {
name "typesafe-ivy-release"
url "https://repo.typesafe.com/typesafe/ivy-releases"
layout "ivy"
}
}
idea {
module {
sourceDirs += file("app")
testSourceDirs += file("test")
scopes.COMPILE = [plus: [configurations.play], minus: []]
scopes.RUNTIME = [plus: [configurations.playRun], minus:[configurations.play]]
scopes.TEST = [plus: [configurations.playTest], minus: [configurations.playRun]]
}
}
PS: tested with intellij 15.0.2 / gradle 2.10 / gradle play plugin
I'm using Intellij (2017 and 2018), Gradle (4.6), and Play (2.5). The three of these do not mix together out of the box.
Without help, IDEA won't recognize dependencies, and will be little more than Notepad -- no code completion, no navigation. This is because IDEA looks at the configuration model of the Java plugin, but the 'play' plugin uses the "new" software model. I don't think either side it going to change this to meet the other any time soon.
The 'idea' plugin writes file-based project files that do not work very well, and have to be refreshed manually. I would advise that you not use the 'idea' plugin at all.
Using 'java' and 'play' plugins, you can help IDEA understand what you want by:
(1)
Moving your Play source files around to follow the pattern expected by the 'java' plugin:
app -> src/main/java
test -> src/test/java
You can make a symbolic link from ./test to src/test/java, or muck with the play model with plugin code. The path ./test is hardcoded (!) in the 'play' plugin, but I find IDEA works better if it finds it in src/test/java.
(2)
Configure the play plugin to get its input from the Java plugin source directories. Something like this:
model {
components {
play {
sources {
java {
source.setSrcDirs files("src/main/java")
}
resources {
source.setSrcDirs files("src/main/resources")
}
routes {
// leave in ./conf
}
}
}
}
}
(3)
Configure the java plugin to put its output in the play plugin output directories. Something like this:
sourceSets {
main {
java.outputDir = file('build/playBinary/classes')
}
test {
java.outputDir = file('build/playBinary/testClasses')
}
}
(4)
Give IDEA a bit more help to figure this out:
configurations {
compile.extendsFrom(play)
testCompile.extendsFrom(playTest)
implementation.extendsFrom(playRun)
implementation.extendsFrom(playPlatform)
}
If you do this, you can then import the Gradle project directly into IDEA, use the Gradle runner for tests, and refresh the gradle project at any time.
I realize this answer comes years late, but there is not a lot of information out there, and this answer may help people still searching for this in the distant future.
+-+-+-+-+-+-+-+-
EDIT: I've stopped using the above approach as it is too unreliable. Instead, I wrote my own plugin to replicate the twirl and routes compiler steps of sbt, and then just use the traditional scala (which uses java) gradle plugin, and also groovy (for spock tests). I use the application plugin to build the distribution. I import it as a gradle build in Intellij and everything "just works". I recommend not using the 'play' plugin at all.
Use the straight-forward gradle plugins. Avoid the 'idea' and 'play' plugins. Import the project into Intellij as a gradle build -- even the traditional JUnit runner works fine. Stay away from the "new" gradle software model in your plugins. Run and debug directly from Intellij as for any other Java application.
This means giving up the "development mode" of sbt, and always running the "ProdServerStart" main (but you can still run in dev or test "mode" of Play). But I'm not missing sbt. :^) When running ProdServerStart, for example, you always run out of a jar with a traditional classpath. You'll need to change the application secret from "changeme", and you will have to put your asset jar on the classpath. This is all straight-forward JVM and no sbt secret sauce (other then twirl and route compilers).
I'll come back with more details but for now:
#TaskAction
public void generate() {
getSource().forEach(sourceFile -> {
TwirlCompiler.compile(sourceFile,
getAppDir(),
getDestinationDir(),
FORMATTER,
ADDITIONAL_IMPORTS,
Codec.UTF8(),
false,
false);
});
}
and
#TaskAction
public void generate() {
getSource().forEach(sourceFile -> {
RoutesCompiler.RoutesCompilerTask routesCompilerTask = new RoutesCompiler.RoutesCompilerTask(sourceFile,
ADDITIONAL_IMPORTS,
true,
true,
false);
RoutesCompiler.compile(routesCompilerTask, INJECTED_ROUTES_GENERATOR, getDestinationDir());
});
}
should get you started if you are comfortable writing gradle plugins in Java.
First of all you have to change your project according to gradle play plugins. here is the links, how to change your sbt project to gradle and make sure that you can execute all the necessary commands(build, clean, runPlayBinary, etc) from command prompt or terminal. When you are able to build successfully then you can import in Intellj IDE. Here is the steps you have to follow. Add idea plugins and following config for idea source configuration in build.gradle file.
plugins {
....
id 'idea'
}
..
idea {
module {
sourceDirs += file("app")
testSourceDirs += file("test")
scopes.COMPILE = [plus: [configurations.play], minus: []]
scopes.RUNTIME = [plus: [configurations.playRun], minus:[configurations.play]]
scopes.TEST = [plus: [configurations.playTest], minus: [configurations.playRun]]
}
}
...
Type "./gradlew idea" to generate idea's necessary project files
Open the project in intellj(Don't import)
Setup sdk(java) from project structure
In IDE at the bottom, you will see message like "Unlinked Gradle project? Import Gradle project, this will also enable Gradle Tool Window. Don't want to see the message for the project again: press here."
click on this message an enable gradle fort this project and setup gradle home and JVM(not project jvm) for gradle
To Run(Run config)
Open run configuration window
Click on(+) and Select Gradle
Name: as you need
Gradle project: your root project or location of your root project
Tasks: runPlayBinary
Go to intellj preference
Go to [Build, Execution and Deployment] section
Select build tool as gradle
Set "Service diretory path" ex: "/Users/.../gradle/gradle-4.10.2"
uncheck "offline work"`
Tested with Grade 4.10.2, IntelliJ IDEA 2018.3.2 (Ultimate Edition) snd Play 2.5.2
I have an Android library project in Eclipse that I am trying to build with Android Studio so I can generate an .aar file for my users.
The project seems to have been imported cleanly into Android Studio using the "Import Project" option in the welcome screen.
How do I now build the module? The instructions on the dev site say that I need to change:
apply plugin: 'android'
to
apply plugin: 'android-studio'
However my build.gradle file doesn't have that line (I would have assumed that the importer would have added it(?)).
If I insert the line apply plugin: 'android-library', and try to 'Sync Project with Gradle Files', I get the error:
Gradle 'MyProject' project refresh failed
Error:C:\Users\Fred\AndroidStudioProjects\MyProject\src\main\AndroidManifest.xml
(The system cannot find the path specified)
The same happens if I try to make the project anyway.
Here's what my build.gradle looks like:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
apply plugin: 'android-library'
My searches to find a solution have failed. Anyone know what is going on?
I'm using the latest Android Studio (0.5.8)
Don't put the apply plugin: 'android-library' statement in the top-level build file. You should find an apply plugin statement in your module-level build file, and you can modify it there as necessary.
Okay. After some head scratching and more searching, I resolved all my problems I think. For the record:
I think my first import broke somehow. Partly because of the problem highlighted by Scott Barta and possibly because my project directory had spaces in it? Not totally sure.
A later import went more smoothly and I was able to sync and build without errors. However, apart from the build output in the Gradle Console, there was no evidence anything had actually happned. The artifact (ie. the .aar file) doesn't show up in the UI.
After finding this question: How to export library to Jar in Android Studio? I realised that the aar file is in fact built. It is just hidden from you. You have to root around in the file system to find the .aar. It is in:
<Library module>/build/libs/
Something that the docs don't tell you anywhere. Sigh.
Hope this helps someone else..
I have installed Scala, sbt, eclipse and IntelliJ Idea 12. And also jdk, jre, etc. I'm able to run scala in Eclipse (Scala eclipse IDE) but I can't do it in Idea, even though I downloaded and installed scala plugin though Idea. Here is what I'm having at File -> Setting
and at a new project creation page
How do I solve these issues?
whereis scala
scala: /usr/bin/scala /usr/bin/X11/scala /usr/share/scala
which scala
/usr/bin/scala
I know I'm repeating this at any possible occasion—but your life will be much easier if you have sbt generate your IDEA project instead of trying to set it up manually. That will take care of configuring the modules correctly, so you are instantly ready to compile and run.
Here is a blog entry that might help. The section "How can I integrate libraries installed by SBT to IDEA?" tells you how to generate the project files.
Basically you need to create—starting from the root directory of your project—the file project/plugins.sbt with the following content:
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.4.0")
(you can also do that in the file ~/.sbt/plugins/build.sbt instead, that way you have the plugin available for any of your projects)
Then you run sbt gen-idea, and afterwards you can open the project directly from IDEA through File -> Open Project (and pointing to the project's root directory).
You could also generate your IDEA project with Gradle, which handles Scala+IDEA combination very well. Here's a minimal build.gradle script to do this:
apply plugin: 'scala'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
compile 'org.scala-lang:scala-library:2.10.1'
}
Just create a directory for your project, put build.gradle inside it, create src/main/scala subdirectory, then install Gradle and run gradle idea inside your project's directory. That should generate nicely configured IDEA project. With this method you don't even need to install Scala.
What exactly your problem is? I don't see anything on your screens which prevents you from using Scala in IDEA. Just select "Set Scala Home" radiobutton in "New project" dialog and then select your Scala installation path (I guess it will be /usr/share/scala). IDEA then will automatically create library and compiler libraries and add Scala facet to your project.