那么如何在 Intellij IDE 中创建 运行 具有依赖关系的可执行 jar,特别是 Android Studio?
How do you create then run an executable jar with dependencies in Intellij IDEs, specifically Android Studio?
我正在尝试通过 gradle 创建并 运行 一个可执行 jar。这就是我当前的 gradle 的样子:
task jarTask(type: Jar) {
baseName = 'my-main-class'
from 'build/classes/main'
}
task createJarWithDependencies(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Sample Jar',
'Implementation-Version': 1,
'Main-Class':'com.example.MyMainClass'
}
baseName = "my-main-class-with-dependencies"
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jarTask
}
configurations {
jarConfiguration
}
artifacts {
jarConfiguration jarTask
}
// This is the task that I call with ./gradlew to execute my jar
task runMyJar(type: JavaExec) {
classpath files('build/libs/my-main-class-with-dependencies.jar')
main 'com.example.MyMainClass'
args = ["param1","param2"]
outputs.upToDateWhen { false }
}
runMyJar.dependsOn(createJarWithDependencies, build)
我从下面的堆栈溢出 answers/references 中得到了这些方法:
Android Studio export jar with dependencies
Android studio - How to use an executable jar file from gradle
然而,当我 运行 ./gradlew clean runMyJar
(甚至只是 ./gradlew runMyJar
,我收到以下错误消息:
Error: Could not find or load main class com.example.MyMainClass
任何人都可以指出为什么我的可执行 jar 在我的 class 中找不到 main 方法的原因吗?有什么我遗漏的吗?
由于我对这个解决方案不太满意,所以我通过执行以下操作设法解决了这个问题:
task deleteJar(type: Delete) {
delete 'libs/my-main-class.jar'
}
task createJar(type: Copy) {
from('build/intermediates/bundles/debug/')
into('libs/')
include('classes.jar')
rename('classes.jar', 'my-main-class.jar')
}
// This is the task that I call with ./gradlew to execute my jar
task runMyJar(type: JavaExec) {
classpath files('libs/my-main-class.jar')
classpath files('libs/common-util.jar')
main 'com.example.MyMainClass'
args = ["param1","param2"]
outputs.upToDateWhen { false }
}
createJar.dependsOn(deleteJar, build)
runMyJar.dependsOn(createJar, build)
虽然这不是我想要的解决方案,因为这种方法不会从该模块引用的其他模块中引入依赖项,而这正是我试图解决的问题。为了做到这一点,我最终做的是 copy/paste 在那些 "other" 模块上使用相同的方法并为该模块生成一个库,然后将该库复制到 libs
文件夹这个模块的。或者我可以将那些依赖代码移动到同一个模块中,这样我就不必处理它了。对于一个还算不错的简单应用程序(幸运的是,我的情况就是这样),但是对于一个复杂的应用程序,我不确定该怎么做。
作为附加参考,这里是我的依赖项部分:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':common-util')
// common-util is an Android module, which references other Android
// methods such as Log.d, android.util class methods, etc.
}
顺便说一下,我正在为此使用 Android Studio IntelliJ IDE(这是我 post 的最初动机)。我希望不必从其他模块中创建单独的 jar 来将其包含到该模块中,而是一次完成所有操作...
我正在尝试通过 gradle 创建并 运行 一个可执行 jar。这就是我当前的 gradle 的样子:
task jarTask(type: Jar) {
baseName = 'my-main-class'
from 'build/classes/main'
}
task createJarWithDependencies(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Sample Jar',
'Implementation-Version': 1,
'Main-Class':'com.example.MyMainClass'
}
baseName = "my-main-class-with-dependencies"
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jarTask
}
configurations {
jarConfiguration
}
artifacts {
jarConfiguration jarTask
}
// This is the task that I call with ./gradlew to execute my jar
task runMyJar(type: JavaExec) {
classpath files('build/libs/my-main-class-with-dependencies.jar')
main 'com.example.MyMainClass'
args = ["param1","param2"]
outputs.upToDateWhen { false }
}
runMyJar.dependsOn(createJarWithDependencies, build)
我从下面的堆栈溢出 answers/references 中得到了这些方法: Android Studio export jar with dependencies Android studio - How to use an executable jar file from gradle
然而,当我 运行 ./gradlew clean runMyJar
(甚至只是 ./gradlew runMyJar
,我收到以下错误消息:
Error: Could not find or load main class com.example.MyMainClass
任何人都可以指出为什么我的可执行 jar 在我的 class 中找不到 main 方法的原因吗?有什么我遗漏的吗?
由于我对这个解决方案不太满意,所以我通过执行以下操作设法解决了这个问题:
task deleteJar(type: Delete) {
delete 'libs/my-main-class.jar'
}
task createJar(type: Copy) {
from('build/intermediates/bundles/debug/')
into('libs/')
include('classes.jar')
rename('classes.jar', 'my-main-class.jar')
}
// This is the task that I call with ./gradlew to execute my jar
task runMyJar(type: JavaExec) {
classpath files('libs/my-main-class.jar')
classpath files('libs/common-util.jar')
main 'com.example.MyMainClass'
args = ["param1","param2"]
outputs.upToDateWhen { false }
}
createJar.dependsOn(deleteJar, build)
runMyJar.dependsOn(createJar, build)
虽然这不是我想要的解决方案,因为这种方法不会从该模块引用的其他模块中引入依赖项,而这正是我试图解决的问题。为了做到这一点,我最终做的是 copy/paste 在那些 "other" 模块上使用相同的方法并为该模块生成一个库,然后将该库复制到 libs
文件夹这个模块的。或者我可以将那些依赖代码移动到同一个模块中,这样我就不必处理它了。对于一个还算不错的简单应用程序(幸运的是,我的情况就是这样),但是对于一个复杂的应用程序,我不确定该怎么做。
作为附加参考,这里是我的依赖项部分:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':common-util')
// common-util is an Android module, which references other Android
// methods such as Log.d, android.util class methods, etc.
}
顺便说一下,我正在为此使用 Android Studio IntelliJ IDE(这是我 post 的最初动机)。我希望不必从其他模块中创建单独的 jar 来将其包含到该模块中,而是一次完成所有操作...