I'm building Kotlin simple Hello-Worl using Gradle
my build.gradle is:
/*
* This build file was generated by the Gradle 'init' task.
*
* This generated file contains a commented-out sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/4.3.1/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Kotlin
apply plugin: 'kotlin'
/*
plugins {
id "org.jetbrains.kotlin.jvm" version "1.1.60"
}
*/
buildscript {
ext.kotlin_version = '1.1.60'
// In this section you declare where to find the dependencies of your project
repositories {
mavenCentral()
// jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
sourceSets {
main.kotlin.srcDirs += 'src/kotlin'
main.resources.srcDirs += 'src/resources'
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.7'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'junit:junit:4.12'
}
kotlin {
experimental {
coroutines 'enable'
}
}
compileKotlin {
kotlinOptions.suppressWarnings = true
}
compileKotlin {
kotlinOptions {
suppressWarnings = true
}
}
and Main.kt is:
fun main(args: Array<String>) {
println("kotlin!")
}
upon running Gradle buil, I got the below error:
Notes:
- I'm new to gradle so built it as below 2 steps:
Step 1:
Step 2:
UPDATE
As per the first answer, I tried getting the files locally, I created another folder named lib and downloaded the *.jar files into it, so I got the gradle.build as below:
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
classpath fileTree(include: ['*.jar'], dir: 'libs')
classpath files('kotlin-gradle-plugin-1.1.60.jar')
}
}
apply plugin: 'kotlin'
sourceSets {
main.kotlin.srcDirs += 'src/kotlin'
main.resources.srcDirs += 'src/resources'
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.7'
compile name: 'kotlin-stdlib-1.1.60'
testCompile 'junit:junit:4.12'
}
kotlin {
experimental {
coroutines 'enable'
}
}
compileKotlin {
kotlinOptions.suppressWarnings = true
}
compileKotlin {
kotlinOptions {
suppressWarnings = true
}
}
Below the revised structure and new error I got:
UPDATE
U copied all the required repositories .jar and .pom to folder:
C:\Users\.m2\repository\org\jetbrains\
I copied for example:
...\kotlin\kotlin-std\1.1.60\kotlin-stdlib-1.1.60.jar
And
...\annotations\13.0\annotations-13.0.jar
And used
mavenLocal()
But still getting the same error :(
I found the issue to be with our company proxy that prevented such thing, so I solved the issue by downloading the required repository in my hole laptop then copied them to the company one.
First, I created a separate folder, named it jars.
After that I downloaded the required file from here and saved it in the jars folder.
Then I installed it into the local repository using the command:
mvn install:install-file -Dfile=utility.jar -DgroupId=com.company -DartifactId=utility -Dversion=0.0.1 -Dpackaging=jar
Such as:
mvn install:install-file -Dfile=kotlin-stdlib-1.1.60.jar -DgroupId=org.jetbrains.kotlin -DartifactId=kotlin-stdlib -Dversion=1.1.60 -Dpackaging=jar
Notes:
To do the above correctly, the maven is required to be downloaded from here and added to the path.
And command above is required to be run from the jars folder, that contains the downloaded repository:
Then I found that the repository had been downloaded into C:\Users\<user>\.m2\ folder:
After copying them into my office laptop, I called them from the mavenLocal():
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile ("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
}
UPDATE
To download the full repository in one shoot instead of downloading the required files one by one, then the following command can be used:
mvn dependency:get -DrepoUrl=something -Dartifact=group:artifact:version
Such as:
mvn dependency:get -DrepoUrl=https://mvnrepository.com/artifact/org.jetbrains.kotlin -Dartifact=org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.60
For some reason 3 files failed to be downloaded, so I got them downloaded manually and installed as per the initial explanation,the files are:
org.jetbrains.kotlin:kotlin-android-extensions:jar:original:1.1.60 => here
org.jetbrains.kotlin:kotlin-compiler-runner:jar:original:1.1.60 => here
org.jetbrains.kotlin:kotlin-build-common:jar:tests:1.1.60 => here
and got them installed using the below commands:
mvn install:install-file -Dfile=kotlin-android-extensions-1.1.60.jar -DgroupId=org.jetbrains.kotlin -DartifactId=kotlin-android-extensions -Dversion=1.1.60 -Dpackaging=jar
mvn install:install-file -Dfile=kotlin-compiler-runner-1.1.60.jar -DgroupId=org.jetbrains.kotlin -DartifactId=kotlin-compiler-runner -Dversion=1.1.60 -Dpackaging=jar
mvn install:install-file -Dfile=kotlin-build-common-1.1.60.jar -DgroupId=org.jetbrains.kotlin -DartifactId=kotlin-build-common -Dversion=1.1.60 -Dpackaging=jar
Considering all the above done, the below build.gradle worked perfectly for me:
// set up the kotlin-gradle plugin
buildscript {
ext.kotlin_version = '1.1.60'
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
// apply the kotlin-gradle plugin
apply plugin: "kotlin"
// add kotlin-stdlib dependencies.
repositories {
mavenLocal() // mavenCentral()
}
dependencies {
//dependencies from a remote repositor
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
//local file, that are not coming from repository, let's say my own jar files
compile files('libs/Display.jar')
compile fileTree(dir: 'libs', include: '*.jar')
}
jar {
manifest {
//Define mainClassName as: '[your_namespace].[your_arctifact]Kt'
attributes ('Main-Class': 'MainKt', "Implementation-Title": "Gradle",
"Implementation-Version": 1)
}
// NEW LINE HERE !!!
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
sourceSets {
main.kotlin.srcDirs += 'src/kotlin'
main.resources.srcDirs += 'src/resources'
}
kotlin {
experimental.coroutines 'enable'
}
compileKotlin {
kotlinOptions.jvmTarget= 1.8 // optional, Minimum jvmTarget of 1.8 needed since Kotlin 1.1
kotlinOptions.suppressWarnings = true
}
Related
On a Linux machine running Java 8 and Gradle 6.3 I need to build a fat jar made of mix of libraries, some sourced from Maven Central, others from a local libs directory located at the root of my repository, together with my build.gradle and the gradlew:
apply plugin: 'java'
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
task copyLibs(type: Copy) {
from configurations.compile
into 'libs'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation "junit:junit:4.12"
}
After running ./gradlew clean build and cd build/libs, if I unzip myproject.jar I can see that no dependencies have been included in the jar.
My end goal is to make my project executable as java -jar myproject.jar.
How can I solve this?
After unzipping your jar file check here for all dependencies.
Your-Project --> BOOT-INF --> libs
by default, if your Gradle build is successful the jar files come here.
you can run java -jar then.
This is a build.gradle that generates a fat jar including local dependencies:
plugins {
id 'java'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
group 'com.mycompany.foo'
version '1.0'
jar {
archiveBaseName = 'myjarname'
archiveVersion = '0.1.0'
manifest {
attributes(
'Main-Class': 'com.mycompany.foo.Main'
)
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
repositories {
mavenCentral()
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation'org.junit.jupiter:junit-jupiter-api:5.7.2'
testImplementation 'org.assertj:assertj-core:3.20.2'
}
I need to use browsermob-proxy.
My project can be run from IDE, but when I build it by gradle and add compile 'net.lightbody.bmp:browsermob-core:2.1.4' into my gradle config file the jar is successfully built (there are no any errors), but the main function is not loaded:
#gradle clean jar
#java -jar build/proxy-0.1.jar
"Error: Could not find or load main class myproject.MainKt"
If I add this utility to my existed package (that could be built and run as a fat jar) it can not be built.
What am I doing wrong?
Is it possible that it is bug in browsermob-proxy?
Thanks.
My gradle file is the following:
group 'proxy'
version '0.1'
buildscript {
ext.kotlin_version = '1.1.3'
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.jetbrains.kotlin', name: 'kotlin-gradle-plugin', version: kotlin_version
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M6'
}
}
apply plugin: 'kotlin'
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'
repositories {
mavenCentral()
jcenter()
maven { url 'https://jitpack.io' }
}
dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib-jre8'
compile 'org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version'
compile 'net.lightbody.bmp:browsermob-core:2.1.4'
testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M6")
testCompile("org.junit.jupiter:junit-jupiter-params:5.0.0-M6")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0-M6")
}
jar {
destinationDir = file('build/')
manifest {
attributes 'Main-Class': 'myproject.MainKt'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
I am trying to build a web application server using WildFly Swarm and the application has to be able to run another Java program inside (I don't want to run it as an external process). I am trying to include the external program as .jar dependency to my web application, however, wildfly-swarm-package task always fails with the following:
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:war
:wildfly-swarm-package FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':wildfly-swarm-package'.
> java.lang.NullPointerException (no error message)
This is my gradle.build file:
buildscript {
version = System.getProperty('swarmVersion') ?: '2016.10.0'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:0.5.6.RELEASE"
classpath "org.wildfly.swarm:wildfly-swarm-plugin:$version"
}
}
apply plugin: "io.spring.dependency-management"
apply plugin: 'wildfly-swarm'
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'war'
//mainClassName = 'org.siret.prank.webapp.rest.Main'
swarm {
properties {
swarm.http.port = 8181
}
}
repositories {
mavenLocal()
mavenCentral()
maven {
url 'https://maven.repository.redhat.com/nexus/content/repositories/releases/'
}
maven {
url 'https://maven.repository.redhat.com/nexus/content/repositories/thirdparty-releases/'
}
flatDir {
dirs 'libs'
}
}
dependencyManagement {
imports {
mavenBom "org.wildfly.swarm:bom-all:$version"
}
}
dependencies {
compile group: 'org.biojava', name: 'biojava-core', version: '4.2.4'
compile group: 'org.biojava', name: 'biojava-structure', version: '4.2.4'
compile "org.wildfly.swarm:jaxrs"
compile group: 'org.wildfly.swarm', name: 'undertow', version: '2016.10.0'
compile 'org.codehaus.groovy:groovy-all:2.4.7'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: 'libs/lib', include: ['*.jar'])
}
task debugJar(dependsOn:"wildfly-swarm-package") << {
javaexec {
main="-jar";
args=[
"-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006",
"build/libs/prank-webapp-swarm.jar"
]
}
}
task runJar(dependsOn:"wildfly-swarm-package") << {
javaexec {
main="-jar";
args=[
"build/libs/prank-webapp-swarm.jar"
]
}
}
War plugin works fine, I can find the jars in WEB-INF/lib directory in the archive.
As an experiment, I tried to empty the libs folder and the error still persists.
Thanks,
Lukas
I migrated to Maven to find out that importing jar to local maven repository is the answer to the problem.
Now, both Maven an Gradle work.
I am working on gradle script where I have two separate list of cognos and other dependencies .
list 1:
cognos:a:10.1.1
cognos:b:10.1.1
cognos:c:10.1.1
cognos:d:10.1.1
com:axis:2.0.3
com:webroot:5.0.3
and List 2:
cognos:a:10.2.2
cognos:b:10.2.2
cognos:c:10.2.2
cognos:d:10.2.2
traven:nt:10.5.0
traven:txtx:5.2.1
I need to compile my source code first with list 1 dependenciesand then list 2 dependencies and publish the artifact with below name in artifactory.
Artifact with list 1 and list 2 dependencies
abc-1.0.0-cognos10.1.1
abc-1.0.0-cognos10.2.2
I can do it with build.gradle but I can do it in two separate build.gradle scripts.I am not sure how we can achieve this goal in signle build.gradle script.Can someone have any idea how to achieve it in single build.gradle
apply plugin: 'java'
version = '1.0'
sourceCompatibility = 1.7
targetCompatibility = 1.7
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.mkyong.DateUtils'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
//Get dependencies from Maven central repository
repositories {
mavenCentral()
}
//Project dependencies
dependencies {
compile 'cognos:a:10.1.1
compile 'cognos:b:10.1.1'
compile 'cognos:c:10.1.1'
compile 'cognos:d:10.1.1'
compile 'traven:nt:10.5.0'
compile 'traven:txtx:5.2.1'
}
In your build.gradle, replace cognos and app version with a parameter:
version = "1.0.0-cognos${cognosVersion}"
sourceCompatibility = 1.7
targetCompatibility = 1.7
...
dependencies {
compile "cognos:a:${cognosVersion}"
compile "cognos:b:${cognosVersion}"
compile "cognos:c:${cognosVersion}"
compile "cognos:d:${cognosVersion}"
}
Then you can manually run this a few times:
gradle build -PcognosVersion=xxxx
If you want to automate that as well, you can write a second gradle driver script release.gradle:
defaultTasks 'performRelease'
task performRelease() << {
['10.1.1','10.2.2'].each{
def tempTask = tasks.create(name: "execute_$it", type: GradleBuild)
tempTask.buildFile='build.gradle'
tempTask.tasks = ['build']
tempTask.startParameter.projectProperties = [cognosVersion: it]
tempTask.execute()
}
}
which you can executes as: gradle -b release.gradle
I currently trying out gradle and am using the java plugin.
However every time I build, I find that the ivy.xml is getting generated as well.
I have not included any other plugin such as the 'ivy-publish' or any other.
Is this gradle's default behaviour?
I can simply ignore it but I was curious regarding why.
Edit: 10Aug2014
apply plugin: 'java'
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
}
}
sourceSets {
mainTwo {
java {
srcDir 'src'
}
}
}
dependencies {
compile fileTree(dir: 'lib', include: '*.jar')
}
uploadArchives {
repositories {
flatDir {
dirs 'dist-two'
}
}
}
task all (dependsOn : [uploadArchives,jar]) {
println 'Done!'
}
task hello << {
println 'Hello Earth'
}