使用 Gradle 的 Java 项目并在 Intellij IDEA 中构建 jar 文件 - 如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37100082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java project with Gradle and building jar file in Intellij IDEA - how to?
提问by Defozo
Is there anywhere a tutorial on how to create Java project with Gradle and then build the jar file?
有没有关于如何使用 Gradle 创建 Java 项目然后构建 jar 文件的教程?
I'm running into various problems with that. When I create Java project and then add Gradle with File -> New -> Module -> Gradle -> ... then I get some errors about Java EE websocket's not available (I'm using Ultimate Edition) but nevermind about that, I managed to create that project by selecting File -> New -> Project... -> Gradle -> ... and now I have my Java project with Gradle working properly (I can debug it in IDE) but when I try to create an artifact (jar file) I get various errors depending on how much I messed up in project structure settings...
我遇到了各种问题。当我创建 Java 项目,然后添加 Gradle with File -> New -> Module -> Gradle -> ... 然后我收到一些关于 Java EE websocket 不可用的错误(我使用的是 Ultimate Edition)但没关系,我通过选择 File -> New -> Project... -> Gradle -> ... 设法创建了该项目,现在我的 Java 项目与 Gradle 正常工作(我可以在 IDE 中调试它)但是当我尝试创建时一个工件(jar 文件)我收到各种错误,具体取决于我在项目结构设置中搞砸了多少......
Buildfile: build.xml does not exist!
Build failed
or
或者
Error: Could not find or load main class Main
I think that this project is such a mess right now that the best solution would be to create another new project and just copy Main.class and Gradle's dependencies into this new project.
我认为这个项目现在一团糟,最好的解决方案是创建另一个新项目,然后将 Main.class 和 Gradle 的依赖项复制到这个新项目中。
But how to make it properly?
但是如何正确制作呢?
回答by Vojtech Ruzicka
回答by f.trajkovski
If you are using Intellij you can just open the Gradle plugin (it is on the right side of your IDE) and execute a command: bootRepackage. With this you will have a jar in: your_project_folder/build/libs.
如果您使用的是 Intellij,则只需打开 Gradle 插件(它位于 IDE 的右侧)并执行命令:bootRepackage。有了这个,您将在:your_project_folder/build/libs 中有一个 jar。
回答by Polar
Just encase you run into no main manifest attribute
problem where the executable Jar
file is not running, I do the following.
如果您遇到no main manifest attribute
可执行Jar
文件未运行的问题,我会执行以下操作。
1: go to your
build.gradle
and add the following at the bottom.
1:转到您的
build.gradle
并在底部添加以下内容。
jar {
manifest {
attributes 'Main-Class': 'your_package_name.YOUR_MAIN_CLASS'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
- go to
View
>Tool Windows
>Gradle
- 去
View
>Tool Windows
>Gradle
- A panel at the right side will be shown, now click the
build
for more task. Under thebuild
task double click thebuild
option and wait until the task is done.
- 将显示右侧的面板,现在单击以
build
获取更多任务。在build
任务下双击该build
选项并等待任务完成。
- Now, go to your project directory and open the
build
>libs
and the executableJar
file will be there.
- 现在,转到您的项目目录并打开
build
>libs
,可执行Jar
文件将在那里。
I'm not sure if this is the right way.
No need to accept if it works, hope this help the others.
我不确定这是否是正确的方法。
如果它有效,则无需接受,希望这对其他人有所帮助。
回答by MrNadimi
Step 1: Add these lines on your build.gradle
第 1 步:将这些行添加到您的build.gradle
jar {
from {
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class': 'YOUR MAIN CLASS'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
Step 2:clean
and build
.
第 2 步:clean
和build
。
I'm sure it will work , Regards
我相信它会起作用,问候