使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:59:59  来源:igfitidea点击:

Java project with Gradle and building jar file in Intellij IDEA - how to?

javajakarta-eeintellij-ideagradlejar

提问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

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


  1. fill GroupId, ArtifactId and version enter image description here
  1. 填写 GroupId、ArtifactId 和 version 在此处输入图片说明


  1. Choose your gradle distribution. You can safely use the reccommended option. enter image description here
  1. 选择您的 gradle 发行版。您可以安全地使用推荐的选项。 在此处输入图片说明


  1. Choose project name and location enter image description here
  1. 选择项目名称和位置 在此处输入图片说明


  1. Click finish
  1. 点击完成


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


  1. You build your sources using Gradle tab → tasks → build (double click) enter image description here
  1. 您可以使用 Gradle 选项卡 → 任务 → 构建(双击)构建源 在此处输入图片说明


  1. You can see the result of the build in the Run tab enter image description here
  1. 您可以在 Run 选项卡中看到构建的结果 在此处输入图片说明


  1. In case of error, you can see details by clicking the following button: enter image description here
  1. 如果出现错误,您可以通过单击以下按钮查看详细信息: 在此处输入图片说明

回答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。

enter image description here

在此处输入图片说明

回答by Polar

Just encase you run into no main manifest attributeproblem where the executable Jarfile is not running, I do the following.

如果您遇到no main manifest attribute可执行Jar文件未运行的问题,我会执行以下操作。

1: go to your build.gradleand 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) } }
}

enter image description here

在此处输入图片说明

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

enter image description here

在此处输入图片说明

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

enter image description here

在此处输入图片说明

  1. Now, go to your project directory and open the build> libsand the executable Jarfile will be there.
  1. 现在,转到您的项目目录并打开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:cleanand build.

第 2 步:cleanbuild

I'm sure it will work , Regards

我相信它会起作用,问候