[英]Creating a Java Gradle project and building the .jar file in IntelliJ IDEA - How to?
Is there a tutorial explaining how to properly create a Java Gradle project and build the.jar file?是否有教程解释如何正确创建 Java Gradle 项目并构建 .jar 文件?
When I create a Java project and add Gradle: File -> New -> Module -> Gradle ->... I receive errors about Java EE websocket's not available (I'm using Ultimate Edition).当我创建一个 Java 项目并添加 Gradle 时:文件 -> 新建 -> 模块 -> Gradle ->... 我收到有关 Java EE websocket 不可用的错误(我使用的是终极版)。 However, I can successfully create a project by selecting File -> New -> Project-> Gradle -> which gives me a Java project with Gradle that I can debug.
但是,我可以通过选择 File -> New -> Project-> Gradle -> 成功创建一个项目,这为我提供了一个可以调试的带有 Gradle 的 Java 项目。 However, when I try to create an artifact (.jar file) I receive errors.
但是,当我尝试创建工件(.jar 文件)时,我收到错误消息。 I assume the errors stem from mistakes I made in the project structure settings.
我假设这些错误源于我在项目结构设置中犯的错误。
Buildfile: build.xml does not exist!
Build failed
or要么
Error: Could not find or load main class Main
My project is such a mess at this point, maybe I should create another project, then copy/paste the Main.class and Gradle's dependencies from the old project onto the new project.此时我的项目一团糟,也许我应该创建另一个项目,然后将 Main.class 和 Gradle 的依赖项从旧项目复制/粘贴到新项目中。
If this is my best option, how do I correctly create the project this time?如果这是我的最佳选择,这次我该如何正确创建项目?
Just incase you run into no main manifest attribute
problem where the executable Jar
file is not running, I do the following.万一您遇到可执行
Jar
文件未运行的no main manifest attribute
问题,我将执行以下操作。
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.将显示右侧的面板,现在单击
build
以获取更多任务。 Under thebuild
task double click thebuild
option and wait until the task is done.在
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.如果有效则无需接受,希望这对其他人有所帮助。
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我相信它会起作用, 问候
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.如果您使用的是 Intellij,则只需打开 Gradle 插件(它位于 IDE 的右侧)并执行命令:bootRepackage。 With this you will have a jar in: your_project_folder/build/libs.
有了这个,您将在以下位置拥有一个 jar:your_project_folder/build/libs。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.