简体   繁体   English

在 IntelliJ IDEA 中创建 Java Gradle 项目并构建 .jar 文件 - 如何?

[英]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?如果这是我的最佳选择,这次我该如何正确创建项目?

  1. Create new Gradle (not Java) project.创建新的 Gradle(不是 Java)项目。 Be sure to select Java option in the dialog.请务必在对话框中选择 Java 选项。 Click Next.点击下一步。 在此处输入图像描述

  1. fill GroupId, ArtifactId and version填写GroupId、ArtifactId和版本在此处输入图像描述

  1. Choose your gradle distribution.选择你的 gradle 发行版。 You can safely use the reccommended option.您可以安全地使用推荐的选项。 在此处输入图像描述

  1. Choose project name and location选择项目名称和位置在此处输入图像描述

  1. Click finish点击完成

  1. You put your java classes to src/main/java folder.您将 java 类放入 src/main/java 文件夹。 When using third party libraries, make sure you declare them as Gradle dependencies.使用第三方库时,请确保将它们声明为 Gradle 依赖项。 在此处输入图像描述

  1. You build your sources using Gradle tab → tasks → build (double click)您使用 Gradle 选项卡 → 任务 → 构建(双击)构建您的源代码在此处输入图像描述

  1. You can see the result of the build in the Run tab您可以在“运行”选项卡中看到构建的结果在此处输入图像描述

  1. In case of error, you can see details by clicking the following button:如果出现错误,您可以单击以下按钮查看详细信息: 在此处输入图像描述

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) } }
}

在此处输入图像描述

  1. go to View > Tool Windows > Gradle转到View > Tool Windows > Gradle

在此处输入图像描述

  1. A panel at the right side will be shown, now click the build for more task.将显示右侧的面板,现在单击build以获取更多任务。 Under the build task double click the build option and wait until the task is done.build任务下双击build选项并等待任务完成。

在此处输入图像描述

  1. Now, go to your project directory and open the build > libs and the executable Jar 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 步: cleanbuild .

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.

相关问题 如何将.jar文件添加到IntelliJ IDEA Java项目中? - How do I add a .jar file into an IntelliJ IDEA Java project? java.lang.NoClassDefFoundError: 在 Intellij Idea 中构建 jar 时的 io/reactivex/subjects/Subject (gradle + JavaFX 11) - java.lang.NoClassDefFoundError: io/reactivex/subjects/Subject when building jar in Intellij Idea (gradle + JavaFX 11) IntelliJ IDEA 2016.3.4 gradle如何运行java项目 - IntelliJ IDEA 2016.3.4 gradle how to run the java project 如何使用intellij IDEA使用gradle项目创建Java servlet - how to create a java servlet with a gradle project using intellij IDEA IntelliJ IDEA 2019 运行 Java 11(在构建 Gradle 项目时)即使每个选项都设置为 Java 8? - IntelliJ IDEA 2019 runs Java 11 (when building Gradle project) even though every option set to Java 8? 如何在 Intellij Idea 中为 Spring Boot 项目构建 jar 文件? - How to build jar file for Spring Boot project in Intellij Idea? IntelliJ IDEA中的Gradle Java调试项目 - Gradle Java debugging project in IntelliJ IDEA Intellij IDEA Java Gradle项目运行 - Intellij IDEA Java Gradle Project Running 如何在Gradle项目中使用IntelliJ IDEA生成包含javadoc的.jar? - How can I generate a .jar that also includes javadoc in it using IntelliJ IDEA in a Gradle project? Java 代码在 Intellij Idea 中工作,但在构建工件后无法工作 JAR 文件 - Java code working in Intellij Idea, but not working JAR file after building artifact
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM