Can't compile Java class when calling Clojure code - java

Adding clojure to an already existing gradle java project
I have a Java project that I want to start trying to add some Clojure to, but I'm hitting a few issues. I'm using the IntelliJ IDEA with the Cursive for Clojure IntelliJ plugin.
I am also using the gradle-clojure plugin for Clojure. My Java classes recognise and can call my Clojure code, but my Java code will no longer compile because at compile time it can no longer see my Clojure code.
Do I need to add an extra step in my build.gradle? Do I need to compile my Clojure separate and manually before trying to compile Java?
Any help would be greatly appreciated.
Environment
gradle-clojure v0.3.1 gradle v4.4.1Java v1.8 Intellij IDE on MacOS High Sierra
Stacktrace <> Task :compileJava FAILED java:8: error: cannot find symbol import com.example.clojure;

You can use Netflix's Clojure Wrapper, nebula.clojure.
Here is an example of a Gradle Build script for a project that uses Java and Clojure:
buildscript {
ext {
springBootVersion = '1.5.8.RELEASE'
}
repositories {
jcenter()
maven { url 'http://clojars.org/repo' }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "com.netflix.nebula:nebula-clojure-plugin:4.4.0"
}
}
plugins {
id "nebula.clojure" version "4.4.0"
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'com.demo'
def artifactName = 'demo-service'
version = latestRepoTag()
static def latestRepoTag() {
def cmd = "git describe --abbrev=0"
def proc = cmd.execute();
return proc.text.trim();
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext['spring-restdocs.version'] = '1.2.1.RELEASE'
ext {
springCloudVersion = 'Dalston.SR4'
}
compileJava {
classpath = project.files(
project.compileClojure.outputs,
classpath
)
}
compileClojure {
jvmOptions {
jvmArgs '-Djava.awt.headless=true'
}
}
repositories {
jcenter()
maven { url 'http://clojars.org/repo' }
}
jar {
baseName = "${artifactName}"
version = latestRepoTag()
}
clojure.aotCompile = true
configurations{
dev
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Dalston.SR3'
}
}
dependencies {
compile 'org.clojure:clojure:1.6.0'
compile('com.google.guava:guava:19.0')
compile("commons-io:commons-io:2.5")
compile "org.apache.pdfbox:pdfbox:2.0.0-RC3"
compile("org.apache.commons:commons-lang3:3.0")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.cloud:spring-cloud-starter-eureka")
compileOnly("org.projectlombok:lombok:1.16.6")
runtime('com.h2database:h2:1.4.190')
runtime("com.ingres.jdbc:iijdbc:10.0-4.0.5")
runtime('org.apache.commons:commons-dbcp2:2.1.1')
runtime('org.postgresql:postgresql:9.4.1209')
dev("org.springframework.boot:spring-boot-devtools")
testCompile('com.jayway.jsonpath:json-path')
testCompile('com.jayway.jsonpath:json-path-assert:2.2.0')
testCompile('com.google.code.gson:gson:2.8.1')
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.springframework.restdocs:spring-restdocs-mockmvc:1.2.1.RELEASE")
}
task wrapper(type: Wrapper) {
gradleVersion = '4.1'
}
bootRun {
classpath = sourceSets.main.runtimeClasspath + configurations.dev
systemProperties = System.properties
jvmArgs = ["-client", "-Dsun.net.inetaddr.ttl=60", "-Djava.security.egd=file:/dev/./urandom"]
environment = [
'spring_profiles_active': 'beta,fast_discovery'
]
}
Here is a link to the plugin on Gradle's plugin documentation site:

Related

Gradle dependency downloads but packages aren't available in project

I published a small library of mine to a free maven hosting service, and am using that package in another project. I've done this before without issue but something isn't working this time.
Gradle finds the file just fine, and downloads it I assume. I know this because any change to the URL, package name, or version, and Gradle throws the "can't find the dep in any of these places" error.
However, any import of the packages in this JAR error, saying it can't find the package. IntelliJ, when I refresh gradle deps, also doesn't show my library in the "External Libraries" section.
Here's my gradle.build for the project I'm using the library in:
apply plugin: 'java'
repositories {
maven { url = 'https://repo.repsy.io/mvn/viveleroi/tileowner' }
}
group = project.property("group")
version = project.property("version")
dependencies {
compileOnly 'network.darkhelmet.tileowner:tileowner:1.0.0'
}
compileJava {
options.compilerArgs += ["-parameters"]
options.fork = true
options.forkOptions.executable = 'javac'
}
The test class:
import network.darkhelmet.tileowner.TileOwner;
public class Demo {}
Gradle says it's on the classpath:
compileClasspath - Compile classpath for source set 'main'.
\--- network.darkhelmet.tileowner:tileowner:1.0.0
compileOnly - Compile only dependencies for source set 'main'. (n)
\--- network.darkhelmet.tileowner:tileowner:1.0.0 (n)
I've downloaded the published jar of my library from that repository, unzipped it, and have confirmed all files are in there as expected.
Here's the build.gradle of my library project:
import org.apache.tools.ant.filters.ReplaceTokens
buildscript {
dependencies {
classpath group: 'com.github.rodionmoiseev.gradle.plugins', name: 'idea-utils', version: '0.2'
}
}
plugins {
id "com.github.johnrengelman.shadow" version "7.0.0"
id "xyz.jpenilla.run-paper" version "1.0.6"
id 'maven-publish'
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'idea-utils'
apply plugin: 'checkstyle'
processResources {
filter ReplaceTokens, tokens: [
"apiversion": project.property("apiversion"),
"version" : project.property("version")
]
}
repositories {
mavenLocal()
mavenCentral()
maven { url = "https://repo.aikar.co/content/groups/aikar/" }
maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
}
group = project.property("group")
version = project.property("version")
targetCompatibility = sourceCompatibility = JavaVersion.VERSION_17
dependencies {
compileOnly 'org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT'
implementation 'co.aikar:acf-bukkit:0.5.0-SNAPSHOT'
}
compileJava {
options.compilerArgs += ["-parameters"]
options.fork = true
options.forkOptions.executable = 'javac'
}
jar {
actions = []
dependsOn = []
dependsOn('shadowJar')
}
shadowJar {
relocate 'co.aikar.commands', 'network.darkhelmet.tileowner.acf'
relocate 'co.aikar.locales', 'network.darkhelmet.tileowner.locales'
}
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
}
}
repositories {
maven {
name = 'repsy'
url = 'https://repo.repsy.io/mvn/viveleroi/tileowner'
credentials(PasswordCredentials)
}
}
}

Gradle Multi-Project Build is Looking Like a Dry Run

So I was given this legacy project which is built with Gradle. This project builds perfectly on linux machines with a simple ./gradlew clean build. However in my windows machine, if I execute the same command the project builts successfully but it is looking like it was a dry run of the build.
I say this because in linux the build takes me around 1 hour while in windows it takes only 50 seconds.
Additionally, I know for a fact that the build on windows is not even running the integration tests nor compiling the java, since some of the imports are even failing.
What makes this most strange is that it works on linux and not on windows.
Please advise.
I will attach the build.gradle maybe it will be useful:
subprojects {
apply plugin: "java"
apply plugin: "idea"
apply plugin: 'org.springframework.boot'
apply plugin: 'org.junit.platform.gradle.plugin'
apply from: "${rootProject.projectDir}/gradle/check/check.gradle"
bootRepackage.enabled = false
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
ext {
buildTag = project.hasProperty('buildTag') ? "-${buildTag}" : ""
assertjVersion = "3.6.2"
junitVersion = "5.3.1"
junitPlatformVersion = "1.3.1"
jitPackVersion = "1.0.0"
mockitoVersion = "2.+"
commonsLang3Version = "3.5"
commonsBeanUtilsVersion = "1.9.3"
mssqlJdbcVersion = "6.1.0.jre8"
flywayVersion = "4.1.2"
swaggerVersion = "2.7.0"
commonsIOVersion = "2.5"
awaitilityVersion = "3.0.0"
cyberSourceClientVersion = "6.2.4"
sshdVersion = "1.6.0"
springSftpVersion = "4.3.10.RELEASE"
jacksonVersion = "2.8.6"
javaxVersion = "3.1.0"
httpclientVersion = "4.5.2"
apiGuardianVersion = "1.0.0"
}
version = "1.1.6${buildTag}"
configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
dependencies {
compile "org.apache.commons:commons-lang3:${commonsLang3Version}"
testCompile "org.assertj:assertj-core:${assertjVersion}"
}
}
task wrapper(type: Wrapper) {
gradleVersion = '3.4.1'
}
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
classpath('org.junit.platform:junit-platform-gradle-plugin:1.0.0-M4')
}
}
EDIT: After running with ./gradlew clean build --info I can see most of the tasks have NO-SOURCE or UP-TO-DATE next to them which does not happen on Linux

Gradle task to compile Java and Groovy sources

We are starting a new Project with gradle (all of my previous projects are on Maven) and this is my first experience on using gradle, below is my build.gradle file and am trying to compile the java and groovy sources using the task compile
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
springVersion = '4.3.7.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
task compile(type: GroovyCompile) {
//source = fileTree(dir: 'src', include: '**/*.java')
sourceSets {
main {
java { srcDirs = [] } // no source dirs for the java compiler
groovy { srcDir "src" } // compile everything in src/ with groovy
}
}
destinationDir = file('build/classes/main')
classpath = files('build/classes/main')
}
dependencies {
compile "org.codehaus.groovy:groovy-all:2.4.10"
compile('org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}')
compile('org.springframework.boot:spring-boot-actuator-docs:${springBootVersion}')
compile('org.springframework.boot:spring-boot-starter-groovy-templates:${springBootVersion}')
compile('org.springframework.boot:spring-boot-starter-jdbc:${springBootVersion}')
compile('org.springframework.boot:spring-boot-starter-jersey:${springBootVersion}')
compile('org.springframework.boot:spring-boot-starter-security:${springBootVersion}')
compile('org.springframework.boot:spring-boot-starter-web:${springBootVersion}')
compile('org.springframework:spring-webmvc:${springVersion}')
compile "com.microsoft:sqljdbc4:4.0"
testCompile('org.springframework.boot:spring-boot-starter-test:${springBootVersion}')
}
And when I run the gradle compile command am seeing :compile NO-SOURCE and no compiled classes in build\classes\main
can someone please help me with gradle task to compile both java and groovy sources?
The Gradle docs of the Groovy plugin describe the default layout like follows. If it is an option to stick to that, there is no need for a custom compile task.
src/main/java Production Java source
src/main/resources Production resources
src/main/groovy Production Groovy sources. May also contain Java sources for joint compilation.
src/test/java Test Java source
src/test/resources Test resources
src/test/groovy Test Groovy sources. May also contain Java sources for joint compilation.
```

Java gradle no service of type styled text output factory available in project scope service

When I build a project in console I have no service of type styled text output factory available in project scope service. I have also a file pom.xml .I don't know what I do it wrong
This is my build.gradle :
import java.sql.Wrapper
buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
maven { url "http://repo.spring.io/libs-milestone" }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
war {
baseName = 'springboot'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-milestone" }
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("javax.servlet:jstl:1.2")
runtime("mysql:mysql-connector-java")
compile("org.springframework.boot:spring-boot-starter-jdbc")
// https://mvnrepository.com/artifact/javax.el/el-api
compile group: 'javax.el', name: 'el-api', version: '2.2.1-b04'
compile ("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper'
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
I believe the problem is a function of the versions of Gradle and Spring's dependency-management-plugin that are in use.
See the original report from the Spring guys in this Bug in Gradle 2.14-rc1 - No service of type StyledTextOutputFactory report. Gradle moved the StyledTextOutputFactory to an internal package at some point (for the 3.0 release), which broke dependency-management-plugin 0.5.x.
This dependency-management-plugin issue details their making changes to address this in their 0.6.0 release.
I see your build script references Gradle 2.3...but I'm wondering if that is accurate.
I think it boils down to either use Gradle 2.x with dependency-management-plugin 0.5.x or use Gradle 3.x with dependency-management-plugin 0.6.x.
Good luck.

Gradle build successful, error running jar

I am new to gradle.
I am building a project using gradle.
It build successfully without any error. While running the build jar file it is giving classNotFoundException.
I am building a simple spring project from spring.io
However question look similar to this but, could not find a solution. Please help.
edit: This is how my build.gradle looks
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("junit:junit")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
startScripts {
mainClassName = 'Application'
}
springBoot {
mainClass = "Application"
}
You'll need to start the application with the generated start scripts. They will automatically take care of setting up the proper classpath.

Categories