Why does Gradle configurations.runtime.collect pull in Groovy? - java

why does the Class-Path include Groovy, and why is it packaged as if it's a run-time dependency? What is pulling Groovy in?
thufir#mordor:~/NetBeansProjects/hello_client$
thufir#mordor:~/NetBeansProjects/hello_client$ gradle clean assemble
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:jar
hello_api-dev.jar groovy-all-2.4.1.jar
:startScripts
:distTar
:distZip
:assemble
BUILD SUCCESSFUL
Total time: 2.401 secs
thufir#mordor:~/NetBeansProjects/hello_client$
The Groovy JAR's are included in the distribution as well:
thufir#mordor:~/NetBeansProjects/hello_client$
thufir#mordor:~/NetBeansProjects/hello_client$ unzip build/distributions/hello_client-dev.zip
Archive: build/distributions/hello_client-dev.zip
creating: hello_client-dev/
creating: hello_client-dev/lib/
inflating: hello_client-dev/lib/hello_client-dev.jar
inflating: hello_client-dev/lib/hello_api-dev.jar
inflating: hello_client-dev/lib/groovy-all-2.4.1.jar
creating: hello_client-dev/bin/
inflating: hello_client-dev/bin/hello_client.bat
inflating: hello_client-dev/bin/hello_client
thufir#mordor:~/NetBeansProjects/hello_client$
thufir#mordor:~/NetBeansProjects/hello_client$ java -jar hello_client-dev/lib/hello_client-dev.jar
hello [fred]
thufir#mordor:~/NetBeansProjects/hello_client$
The build.groovy file:
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'maven'
mainClassName = 'net.bounceme.mordor.hello.client.HelloClient'
version = 'dev'
group = 'com.some.project'
description = 'hello world KISS'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
if (!hasProperty(mainClassName)) {
ext.mainClass = mainClassName
}
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
compile 'com.github.THUFIR:hello_api:dev'
}
jar.doFirst
{
def manifestClasspath = configurations.runtime.collect { it.name }
manifestClasspath = manifestClasspath.unique().join(" ")
println (manifestClasspath)
manifest.attributes.put("Main-Class", mainClassName)
manifest.attributes.put("Class-Path", manifestClasspath)
}
I think that the solution lies in dealing with configurations.runtime.collect in that Groovy should be removed from that collection. It just seems odd that it's included implicitly. Is this typical, to include Groovy?
The notion of a providedCompile looks interesting, but I'm not sure that it applies. Am I using Gradle in an odd way somehow?
The library and client repos are on github. Code posted might not be 100% as the latest commits, to make the questions as clear as possible.

Related

no main manifest attribute, in jar in Intellij Idea, using Gradle

Same thing works in my other project, but here I get an error :
no main manifest attribute, in jar
here's my Gradle :
group 'com.vladdrummer.txtadventureserver'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile "com.sparkjava:spark-core:2.7.2"
}
Here's where manifest is :
So, it seems to be in a right place.
The manifest itself :
Manifest-Version: 1.0
Main-Class: TextAdventureServer
and the Artifact:
So, it seems to me that everything is in place, but when I build Artifact and try java -jar txtadventureserver_main.jar - it shows me error:
no main manifest attribute, in txtadventureserver_main.jar
Where did I make a mistake?

set Main-Class header with gradle from mainClassName

how do I get the jar task to set the Main-Class header from the mainClassName variable?
thufir#dur:~/NetBeansProjects/props$
thufir#dur:~/NetBeansProjects/props$ gradle clean assembleDist;java -jar build/libs/props.jar
BUILD SUCCESSFUL in 1s
6 actionable tasks: 6 executed
Publishing build scan...
https://gradle.com/s/qwirajeu4guhq
no main manifest attribute, in build/libs/props.jar
thufir#dur:~/NetBeansProjects/props$
thufir#dur:~/NetBeansProjects/props$ cat build.gradle
plugins {
id 'com.gradle.build-scan' version '1.8'
id 'java'
id 'application'
}
buildScan {
licenseAgreementUrl = 'https://gradle.com/terms-of-service'
licenseAgree = 'yes'
}
buildScan {
publishAlways()
}
sourceCompatibility = 1.9
targetCompatibility = 1.9
mainClassName = 'net.bounceme.dur.props.Main'
jar {
manifest {
// attributes 'Main-Class': mainClassName
attributes 'Class-Path': configurations.compile.collect { it.getName() }.join(' ')
}
}
repositories {
jcenter()
}
dependencies {
// This dependency is found on compile classpath of this component and consumers.
// compile 'com.google.guava:guava:22.0'
// Use JUnit test framework
// testCompile 'junit:junit:4.12'
}
thufir#dur:~/NetBeansProjects/props$
version '1.0'
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'blah.blah.blah.blah.myMain'
jar {
manifest {
attributes(
'Main-Class': mainClassName
)
}
}
This will result in a MANIFEST.MF of :
Manifest-Version: 1.0
Main-Class: blah.blah.blah.blah.myMain

Cannot install library using gradle

This is my build file. I'm using Intellij CE. I'm trying to use this yahoo finance api:
group 'com.scrap'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.yahoofinance-api', name: 'YahooFinanceAPI', version: '4.0.0'
compile 'joda-time:joda-time:2.2'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
When I manually sync the project in the Gradle tool window, I get a red line under the yahoofinance library and a message that says
unable to resolve com.yahoofinance-api:YahooFinanceAPI:4.0.0
What am I doing wrong?
4.0 YahooFinanceAPI does not exist in Maven. The latest one is 3.5.0 - cf - https://mvnrepository.com/artifact/com.yahoofinance-api/YahooFinanceAPI
Tested by modify the version and adding an option for creating a fat jar
group 'com.scrap'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.yahoofinance-api', name: 'YahooFinanceAPI', version: '3.5.0' //Changed version
compile 'joda-time:joda-time:2.2'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
// Added new section
jar {
from {
(configurations.runtime).collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes("Main-Class": "Main" )
}
}
Ran gradle clean build and the dependencies download
$ gradle clean build
Download https://repo1.maven.org/maven2/com/yahoofinance-api/YahooFinanceAPI/3.5.0/YahooFinanceAPI-3.5.0.pom
Download https://repo1.maven.org/maven2/com/yahoofinance-api/YahooFinanceAPI/3.5.0/YahooFinanceAPI-3.5.0.jar
Download https://repo1.maven.org/maven2/joda-time/joda-time/2.2/joda-time-2.2.jar
:clean
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 10.199 secs

spring boot gradle app on ubuntu: unable to access jarfile

I have an existing Spring app that I want to convert to a Spring Boot app. So I have added th Application.java file in my java source directory and execute my gradlwe script as usual. However, when I run the build command, a get an error saying 'unable to access jarfile' - although I specify the jarfile name in my build script and on my command line. Below is the build.gradle file and the output. What is wrong here?
build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'spring-boot'
archivesBaseName = 'spring4'
version = '1'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web:1.3.3.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.3.3.RELEASE'
compile 'org.hibernate:hibernate-core:4.3.6.Final'
compile 'mysql:mysql-connector-java:5.1.31'
compile 'commons-dbcp:commons-dbcp:1.4'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat:1.3.3.RELEASE'
compile "ch.qos.logback:logback-classic:1.1.3"
compile "org.slf4j:log4j-over-slf4j:1.7.13"
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
// end::jetty[]
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
// end::actuator[]
testCompile("junit:junit")
}
jar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}
configurations.all {
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j"
}
sourceSets {
main {
resources {
srcDirs "src/main/resources"
}
}
}
output:
vagrant#vagrant:/vagrant/Spring4RESTAngularJS$ ./gradlew build && java -jar build/libs/gs-spring-boot-0.1.0.jar
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:war
:bootRepackage
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 11.772 secs
Error: Unable to access jarfile build/libs/gs-spring-boot-0.1.0.jar
vagrant#vagrant:/vagrant/Spring4RESTAngularJS$
check target directory:
vagrant#vagrant:/vagrant/Spring4RESTAngularJS$ ll build/libs/
total 53697
drwxrwxrwx 1 vagrant vagrant 0 Sep 15 11:25 ./
drwxrwxrwx 1 vagrant vagrant 4096 Sep 13 09:54 ../
-rwxrwxrwx 1 vagrant vagrant 30698749 Sep 15 11:25 spring4-1.war*
-rwxrwxrwx 1 vagrant vagrant 24281990 Sep 15 11:24 spring4-1.war.original*
vagrant#vagrant:/vagrant/Spring4RESTAngularJS$

how to specify run time dependencies for a fat JAR, using Groovy

A very brief search explained to how to package run time dependencies for distribution:
thufir#mordor:~/NetBeansProjects/hello_client$
thufir#mordor:~/NetBeansProjects/hello_client$ gradle clean build
Changed strategy of configuration ':compile' after it has been resolved. This behaviour has been deprecated and is scheduled to be removed in Gradle 3.0
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:startScripts
:distTar
:distZip
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 9.237 secs
thufir#mordor:~/NetBeansProjects/hello_client$
thufir#mordor:~/NetBeansProjects/hello_client$ java -jar build/libs/hello_client.jar
hello [fred]
thufir#mordor:~/NetBeansProjects/hello_client$
with the desired output. However, the suggested solution, a "fat jar", results in this monstrosity:
thufir#mordor:~/NetBeansProjects/hello_client$
thufir#mordor:~/NetBeansProjects/hello_client$ jar -ft build/libs/hello_client.jar
META-INF/
META-INF/MANIFEST.MF
net/
net/bounceme/
net/bounceme/mordor/
net/bounceme/mordor/hello/
net/bounceme/mordor/hello/client/
net/bounceme/mordor/hello/client/HelloClient.class
net/bounceme/mordor/hello/library/
net/bounceme/mordor/hello/library/HelloLibrary.class
META-INF/ANTLR-LICENSE.txt
META-INF/ASM-LICENSE.txt
META-INF/CLI-LICENSE.txt
META-INF/JSR223-LICENSE.txt
META-INF/LICENSE.txt
META-INF/NOTICE.txt
META-INF/dgminfo
META-INF/groovy-release-info.properties
META-INF/maven/
META-INF/maven/commons-cli/
META-INF/maven/commons-cli/commons-cli/
META-INF/maven/commons-cli/commons-cli/pom.properties
META-INF/maven/commons-cli/commons-cli/pom.xml
META-INF/services/
META-INF/services/javax.script.ScriptEngineFactory
META-INF/services/org.codehaus.groovy.plugins.Runners
META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
META-INF/services/org.codehaus.groovy.source.Extensions
META-INF/services/org.codehaus.groovy.transform.ASTTransformation
groovy/
groovy/beans/
groovy/beans/Bindable.class
groovy/beans/BindableASTTransformation.class
which then goes on seemingly endlessly. Obviously, I don't want to include groovy in the fat jar. How can I exclude groovy from the built JAR?
build file:
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'maven'
mainClassName = 'net.bounceme.mordor.hello.client.HelloClient'
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
if (!hasProperty(mainClassName)) {
ext.mainClass = mainClassName
}
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.10'
compile 'com.github.THUFIR:hello_api:dev'
}
jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest {
attributes ('Main-Class': mainClassName,
"Class-Path": configurations.compile.collect { it.getName() }.join(' '))
}
}
assemble.dependsOn (jar)
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
what alternatives are there to:
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
Admittedly, I haven't learned the Groovy DSL and am just kludging together bits and pieces so far.
I believe that I saw mention of subtracting one set of dependencies from another to minimize the included libraries. However, in the odd case that there are libraries which aren't required to compile, but are required to execute the JAR, I'm not sure how that would work.

Categories