如何从 IntelliJ IDEA 正确构建 JAR?
我有一个包含单个模块和一些依赖项的项目。 我想在一个单独的目录中创建一个 JAR,其中包含已编译的模块。 此外,我希望在我的模块旁边存在依赖项。
无论我如何扭转 IntelliJ 的“构建 JAR”过程,我的模块的输出都显示为空(除了 META-INF
文件)。
I have a project that contains a single module, and some dependencies.
I'd like to create a JAR, in a separate directory, that contains the compiled module. In addition, I would like to have the dependencies present beside my module.
No matter how I twist IntelliJ's "build JAR" process, the output of my module appears empty (besides a META-INF
file).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
通过 Maven,你可以使用这个插件:
With Maven you can use this plugin:
在 IntelliJ 终端中使用
上面的命令。 它生成一个带有可执行 Jar 的目标文件。
Using
type above command inside IntelliJ Terminal. It generates a Target file with Executable Jar.
Idea 8.1.3
Jar 没问题,因为“output”目录(project/out/product//)中有已编译的输出,
我猜,您必须在构建
依赖项的 jar 之前运行“make”,只需检查“show library”并选择你想要什么。
Idea 8.1.3
Jar is ok, since there is compiled output in 'output' directory (project/out/production//)
I guess, you have to run 'make' before building jar
for dependencies just check "show library" and choose what you want.
这里是IntelliJ IDEA 2018.3 Help的官方解答。 我尝试过并且成功了。
Here is the official answer of IntelliJ IDEA 2018.3 Help. I tried and It worked.
我的问题是这样的:我使用Intellij IDEA(尝试了不同的版本)+ Gradle。
当我编译项目并构建 artifaf jar 时,如上面的响应所示,我收到一个错误 - “没有主清单属性......”
这个解决方案对我有用(特别感谢 Thilina Ashen Gamage(见上文)的提示) :
摘录 - 如果您使用外部库并通过项目设置 - 工件 - 添加 (+) - Jar - 来自具有依赖项的模块构建项目,那么可能是因为 META-INF 文件夹中的程序错误MANIFEST_MF 文件不包括 jar。 为了避免它创建空 jar 文件。
项目设置 - 工件 - 添加 (+) - Jar - EMPTY JAR。 META-INF 文件夹将添加到资源文件夹中。 然后选择您的主课程。 您将看到以下 jar 结构:
请注意文件夹 META-INF 的存在
然后您可以构建项目并构建工件。 该解决方案也适用于 javaFX 应用程序。
My problem was this: I used Intellij IDEA (tried different versions) + Gradle.
When I compiled the project and builded artifacf jar, as indicated in the above responses, I received an error - "no main manifest attrubute ..."
This solution worked for me (special thanks to Thilina Ashen Gamage (see above) for tip):
Excerpt - if you use external libraries and build a project through Project Settings - Artifacts - Add (+) - Jar - From modules with dependencies, then probably because of a program bug the META-INF folder with MANIFEST_MF file not including to jar. To avoid it create EMPTY jar file.
Project Settings - Artifacts - Add (+) - Jar - EMPTY JAR. META-INF folder will added to resources folder. Then select your main class. You will see following jar structure:
Note the presence of a folder META-INF
Then you can build your project and build artifacts. This solution worked for javaFX applications too.
您可能想看看 Maven (http://maven.apache.org)。 您可以将其用作应用程序的主要构建过程,也可以仅通过“编辑配置”对话框执行某些任务。 如果您希望将所有依赖项包含在一个自可执行 JAR 中,那么在 Maven 中创建模块 JAR 的过程相当简单。
如果您以前从未使用过 Maven,那么您需要阅读使用 Maven 进行更好的构建。
You might want to take a look at Maven (http://maven.apache.org). You can use it either as the main build process for your application, or just to perform certain tasks through the Edit Configurations dialog. The process of creating a JAR of a module within Maven is fairly trivial, if you want it to include all the dependencies in a self-executable JAR that is trivial as well.
If you've never used Maven before then you want to read Better Builds With Maven.
如果您在项目中使用第三方库,或者在正确创建 MANIFEST.MF 文件时遇到问题,则在运行使用
上述方法生成的 JAR 文件时可能会出现冲突。
相反,我建议您创建一个空 JAR 并手动将所有其他元素添加到输出根目录。 有关此方法的精彩博客文章可以在这里找到: http://karthicraghupathi.com/2016/07/10/creating-an-executable-jar-in-intellij-idea/ 我尝试了那里提到的步骤,一切对我来说都很好!
If you are using third party libraries with your project or if you have problems with creating MANIFEST.MF file properly, there can be conflicts when running JAR files generated using
method mentioned above.
Instead I suggest you to create an empty JAR and add all other elements to the output root manually. A wonderful blog article for this method can be found here: http://karthicraghupathi.com/2016/07/10/creating-an-executable-jar-in-intellij-idea/ I tried the steps mentioned there and everything worked fine for me!
其他一些答案是无用的,因为一旦您从 Maven 项目重新导入 IntelliJ IDEA 项目,所有更改都将丢失。
jar 的构建需要由运行/调试配置触发,而不是由项目设置触发。
Jetbrains 在这里对如何完成此任务有一个很好的描述:
https://www.jetbrains .com/help/idea/maven.html
向下滚动到名为“配置 Maven 目标的触发器”的部分。
(他们描述的唯一缺点是他们的屏幕截图采用默认的黑白配色方案,而不是超级棒的 darcula 主题。呃!)
所以,基本上,你要做的就是打开“Maven 项目”面板中,您会找到感兴趣的项目(在您的情况下,是构建 jar 的项目),在它下面您会找到要执行的 Maven 目标(通常是“package”目标创建 jars),您打开它的上下文菜单(在 Windows 计算机上右键单击),您可以选择“在运行/调试之前执行...”选项,它将引导您从那里开始。 真的很容易。
Some of the other answers are useless because as soon as you re-import the IntelliJ IDEA project from the maven project, all changes will be lost.
The building of the jar needs to be triggered by a run/debug configuration, not by the project settings.
Jetbrains has a nice description of how you can accomplish this here:
https://www.jetbrains.com/help/idea/maven.html
Scroll down to the section called "Configuring triggers for Maven goals".
(The only disadvantage of their description is that their screenshots are in the default black-on-white color scheme instead of the super-awesome darcula theme. Ugh!)
So, basically, what you do is that you open the "Maven Projects" panel, you find the project of interest, (in your case, the project that builds your jar,) underneath it you find the maven goal that you want to execute, (usually the "package" goal creates jars,) you open up the context menu on it, (right-click on a Windows machine,) and there will be an "Execute before Run/Debug..." option that you can select and it will take you by the hand from there. Really easy.
如果您尝试使用 kotlin 构建 jar,则需要创建一个 src/main/java 文件夹并使用该文件夹作为 META-INF 文件夹的位置。
In case you are trying to build a jar with kotlin you need to create a
src/main/java
folder and use this folder as a location for the META-INF folder.如果您正在阅读此答案,因为您遇到“找不到资源文件”错误,请尝试以下操作:
输出布局
中,按创建存档
创建一个新的jar,然后再次给它一个好听的名字;)添加库文件
,然后选择您需要的库(可以使用Ctrl+A全选)。In case you are reading this answer because you are facing "resource file not found" error, try this:
Output Layout
, pressCreate Archive
to create a new jar, and again, give it a nice name ;)Manifest File
then set correctMain Class
andClass Path
.Add Library Files
, and select libraries you need (you can use Ctrl+A to select all).使用 Gradle 构建您的项目,然后 jar 位于构建文件中。 以下是有关如何使用 Gradle 构建 .jar 文件的 Stack Overflow 链接。
使用 Gradle 和构建的 Java 项目Intellij IDEA 中的 jar 文件 - 如何?
Use Gradle to build your project, then the jar is within the build file. Here is a Stack Overflow link on how to build .jar files with Gradle.
Java project with Gradle and building jar file in Intellij IDEA - how to?
我尝试了大部分给出的答案,但在我的情况下,“out”文件夹(包含 jar 文件)没有被创建。 这样就完成了工作:
构建 -> 构建工件 -> (你会看到类名)-> 现在构建
,“out”文件夹已创建,jar 文件位于其中。
I tried most of the given answers, but in my case "out" folder (which contains jar file) was not getting created. So this did the job :
Build -> Build Artifacts -> (you will see the class name) -> Build
Now, "out" folder was created and jar file was in it.
如果您使用的是新的测试版 IntelliJ UI,则上述解决方案将需要修改。
目前,在新的测试版 IntelliJ UI 中,存在一个错误:如果您单击构建工件菜单,什么也不会发生。
但是,如果您使用箭头键导航到“构建”按钮,然后按键盘上的“Enter”键,它将起作用。
或者您可以执行以下操作:
错误跟踪器:https:/ /youtrack.jetbrains.com/issue/IDEA-316889/Build-artifacts-inactive-if-hamburger-menu-is-on
希望这可以帮助某人避免我在尝试将 JAR 文件获取到时所经历的痛苦被建造。
If you're using the new beta IntelliJ UI, the above solutions will require modification.
Currently, in the new beta IntelliJ UI, there is a bug where if you click on the "build" button in the build artifacts menu, nothing will happen.
However, if you use arrow keys to navigate to the "build" button, and then press "enter" on your keyboard, it will work.
or you can do the following:
Bug tracker: https://youtrack.jetbrains.com/issue/IDEA-316889/Build-artifacts-inactive-if-hamburger-menu-is-on
Hopefully this saves someone from the hour of pain I experienced trying to get my JAR file to be built.
如果您正在处理 spring/mvn 项目,您可以使用以下命令:
jar 文件将保存在目标目录中。
If you are working on spring/mvn project you can use this command:
The jar file will be saved on target directoy.
Ant和Maven被广泛使用。 我更喜欢 Ant,我觉得它更轻量级,并且开发人员可以更好地控制。 有些人会认为这是它的缺点:-)
Ant and Maven are widely used. I prefer Ant, I feel it's more lightweight and you the developer are more in control. Some would suggest that's its downside :-)
说明:
文件 -> 项目结构 -> 项目设置 -> 文物 -> 单击+(加号)-> 罐子 -> 从具有依赖关系的模块...
如果需要使 jar 可运行,请选择一个主类(带有
main()
方法的类)。选择提取到目标 Jar
单击确定
单击应用/确定
上面将“骨架”设置为jar 将被保存到。 要实际构建并保存它,请执行以下操作:
Build -> 构建工件 -> 构建
尝试从以下位置提取 .jar 文件:
Instructions:
File -> Project Structure -> Project Settings -> Artifacts -> Click + (plus sign) -> Jar -> From modules with dependencies...
Select a Main Class (the one with
main()
method) if you need to make the jar runnable.Select Extract to the target Jar
Click OK
Click Apply/OK
The above sets the "skeleton" to where the jar will be saved to. To actually build and save it do the following:
Build -> Build Artifact -> Build
Try Extracting the .jar file from:
References:
这在2017年仍然是一个问题,我希望它能帮助那里的人!
我发现在 IntelliJ 2017.2 1 下创建工作 jar-s 的两种可能性
。 从 IntelliJ 创建工件:
< img src="https://i.sstatic.net/wEbRv.png" alt="创建新工件">
您必须更改清单目录:
将“java”替换为“resources”
这应该是这样的:
然后选择要打包到 jar 中的依赖项,或靠近您的 jar 文件
要构建您的工件,请转到构建工件并选择“重建”。 它将创建一个包含您的 jar 文件及其依赖项的“out”文件夹。
2. 使用 maven- assembly-plugin
将构建部分添加到 pom 文件
此过程将在“target”文件夹下创建 jar 文件
This is still an issue in 2017, I hope it will help somebody out there!
I found 2 possibilities to create working jar-s under IntelliJ 2017.2
1. Creating artifact from IntelliJ:
You have to change manifest directory:
replace "java" with "resources"
This is how it should look like:
Then you choose the dependencies what you want to be packed IN your jar, or NEAR your jar file
To build your artifact go to build artifacts and choose "rebuild". It will create an "out" folder with your jar file and its dependencies.
2. Using maven-assembly-plugin
Add build section to the pom file
This procedure will create the jar file under the "target" folder
我最近遇到了这个问题,并且认为如果任何先前的解决方案或链接缺少细节,这些步骤很容易遵循。
如何使用 IntelliJ IDEA 14.1.5 创建
.jar
:I recently had this problem and think these steps are easy to follow if any prior solution or link is missing detail.
How to create a
.jar
using IntelliJ IDEA 14.1.5:对于那些像我一样受益于图像的人:
文件 -> 项目结构
For those that benefit from images as I do:
File -> Project Structure
下面是 2 个使用 maven 项目的示例,一步一步进行:
方法 1:使用 maven 和 pom.xml 构建 jar 文件
在 pom.xml 中添加 maven-jar-plugin。
<前><代码> <插件>
org.apache.maven.plugins
maven-jar-plugin ;
true
lib/ ;
somePackage.sample
<版本>3.1.0
<配置>
<存档>
<清单>
屏幕截图:
4.通过单击搜索图标并输入maven,打开maven项目框,
单击“清理”,然后单击“安装”
你的 jar 文件将显示在目标文件夹中
使用测试你的罐子
java -jar
方法2:使用maven构建jar,无需更改pom.xml
非常重要!
MANIFEST.MF 需要位于您的资源文件夹中,并且 Main.Class 需要引用 {package}-{class-name-that-contains-main-class}
Here are 2 examples with maven project, step by step:
Method 1: Build jar with maven and pom.xml
at pom.xml, add maven-jar-plugin.
screen capture:
4. open maven project box by click on the search icon and type maven,
click on clean, then click on install
your jar file will show up inside the target folder
test your jar using
java -jar
Method 2: Build jar with maven without pom.xml change
Very important!
The MANIFEST.MF needs to be in your resources folder and the Main.Class needs to refer to {package}-{class-name-that-contains-main-class}
当我使用这些解决方案时出现此错误:
解决方案是:
您必须更改清单目录:
将 java 更改为资源
When i use these solution this error coming:
and solution is:
You have to change manifest directory:
change java to resources
可能有点晚了,但我设法这样解决了 ->
用winrar打开并删除META-INF文件夹中的ECLIPSEF.RSA和ECLIPSEF.SF,并在MANIFEST.MF中放入“Main-class: main_class_name”(不带“.class”)。 确保在最后一行后按两次“Enter”键,否则它将无法工作。
It is probably little bit late, but I managed to solve it this way ->
open with winrar and delete ECLIPSEF.RSA and ECLIPSEF.SF in META-INF folder, moreover put "Main-class: main_class_name" (without ".class") in MANIFEST.MF. Make sure that you pressed "Enter" twice after the last line, otherwise it won't work.
我试图从多模块 Kotlin 应用程序构建一个 jar,但遇到了臭名昭著的“无主清单属性”错误。 在 src/main/resources 中创建清单目录也不起作用。
相反,当直接在
src
中创建它时,它会起作用:src/META-INF/MANIFEST.MF
。I was trying to build a jar from a multi-module Kotlin app and was getting the notorious 'no main manifest attribute' error. Creating the manifest directory in
src/main/resources
didn't work either.Instead, it works when creating it in
src
directly:src/META-INF/MANIFEST.MF
.正如上面的人所说,但我必须注意一点。
您必须选中该复选框:
As the people above says, but I have to note one point.
You have to check the checkbox: