如何使用gradle将配置文件或任何其他资源添加到我的jar中?
我的项目结构:
src/main/java/com/perseus/.. --- Java包(源文件)
src/main/java/config/*.xml --- Spring配置文件
预期的罐子结构:
com/perseus/.. --- Java包(类文件)
config/*.xml --- Spring配置文件
正如标题所述,我想知道如何修改它gradle.build.kts以便创建一个独特jar的所有依赖项(包括kotlin lib)的任务.
我在Groovy中找到了这个示例:
//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
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在kotlin中写出来,除了:
task("fatJar") {
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用kotlin和gradle,但我无法使用Intellij Idea 15成功创建项目.
我已经创建了有两个模块简单的项目hello-java和hello-kotlin.
hello-java 是普通的java项目,它正在编译和运行完美.
hello-kotlin是简单的kotin模块,只有一个*.kt文件和build.gradle文件.
以下是来源:
的build.gradle
group 'pl.fzymek.kotlin'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:0.10.4"
}
}
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName = 'HelloKotlinKt'
repositories {
mavenCentral()
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:0.10.4"
}
Run Code Online (Sandbox Code Playgroud)
HelloKotlin.kt
fun main(args: Array<String>) {
println("Hello, Kotlin!")
}
Run Code Online (Sandbox Code Playgroud)
主模块settings.gradle
include 'hello-java'
include 'hello-kotlin'
Run Code Online (Sandbox Code Playgroud)
运行时gradlew clean build所有项目都编译成功,但运行时出现java -jar hello-kotlin-1.0-SNAPSHOT.jar …
最简单的Kotlin hello world for gradle script Kotlin:
thufir@dur:~/github/gradleScriptKotlin$
thufir@dur:~/github/gradleScriptKotlin$ gradle clean shadowJar;java -jar build/libs/gradleScriptKotlin.jar
> Task :compileKotlin
Using Kotlin incremental compilation
> Task :shadowJar
A problem was found with the configuration of task ':shadowJar'. Registering invalid inputs and outputs via TaskInputs and TaskOutputs methods has been deprecated and is scheduled to be removed in Gradle 5.0.
- No value has been specified for property 'mainClassName'.
The SimpleWorkResult type has been deprecated and is scheduled to be removed in Gradle …Run Code Online (Sandbox Code Playgroud) 我尝试使用 Intellij Idea 和 Kotlin Gradle 项目构建一个 jar。当我尝试配置 Artifact 时,Idea 看不到我的主类
这是我的摇篮:
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.60'
id 'application'
}
group 'org.vladdrummer'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin/'
}
jar {
manifest {
attributes 'Main-Class': 'MovieQuizBackendKt'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile "com.sparkjava:spark-kotlin:1.0.0-alpha"
implementation 'com.google.code.gson:gson:2.8.2'
runtime 'mysql:mysql-connector-java:5.1.34'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Run Code Online (Sandbox Code Playgroud)
这是结构: …
在Gradle(5.0+)中有两种创建方法,即任务:
tasks {
val javadocJar by creating(Jar::class) {
val javadoc by tasks
from(javadoc)
classifier = "javadoc"
}
}
Run Code Online (Sandbox Code Playgroud)
和
tasks {
val javadocJar by registering(Jar::class) {
val javadoc by tasks
from(javadoc)
classifier = "javadoc"
}
}
Run Code Online (Sandbox Code Playgroud)
基本上是相同的API,有什么区别?
我有一个配置了Gradle和Kotlin的项目。这是一个命令行实用程序,我希望能够从终端运行生成的jar。但是我收到以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at com.autentia.impt.MainKt.main(Main.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Run Code Online (Sandbox Code Playgroud)
我的gradle配置如下:
buildscript {
ext.kotlin_version = '1.2.20'
ext.junit_platform_version = '1.0.1'
ext.junit_version = '5.0.0'
ext.moshi_version = '1.5.0'
ext.jna_version = '4.5.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.junit.platform:junit-platform-gradle-plugin:$junit_platform_version"
}
}
version '1.0-SNAPSHOT'
apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: 'org.junit.platform.gradle.plugin'
mainClassName = 'com.autentia.impt.MainKt'
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.jetbrains.kotlin:kotlin-reflect"
implementation "com.squareup.moshi:moshi:$moshi_version" …Run Code Online (Sandbox Code Playgroud)