Java 如何正确地从 IntelliJ 构建 jars?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1082580/
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
How to build jars from IntelliJ properly?
提问by ripper234
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'd like to have the dependencies present beside my module.
我有一个包含单个模块和一些依赖项的项目。我想在单独的目录中创建一个 jar,其中包含已编译的模块。此外,我希望在我的模块旁边有依赖项。
No matter how I twist IntelliJ's "build jar" process, the output of my module appears empty (besides a META-INF file).
无论我如何扭曲 IntelliJ 的“构建 jar”过程,我的模块的输出都显示为空(除了 META-INF 文件)。
采纳答案by simao
Here's how to build a jar with IntelliJ 10 http://blogs.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/
这是使用 IntelliJ 10 构建 jar 的方法http://blogs.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/
File-> Project Structure-> Project Settings-> Artifacts-> Click green plus sign-> Jar-> From modules with dependencies...
File-> Project Structure-> Project Settings-> Artifacts-> 点击green plus sign-> Jar->From modules with dependencies...
The above sets the "skeleton" to where the jar will be saved to. To actually build and save it do the following:
以上将“骨架”设置为 jar 将保存到的位置。要实际构建和保存它,请执行以下操作:
Extract to the target Jar
提取到目标 Jar
OK
好的
Build | Build Artifact | Build
构建 | 构建工件 | 建造
Try Extracting the .jar file from
尝试从中提取 .jar 文件
ProjectName | out | artifacts | ProjectName_jar | ProjectName.jar
项目名称 | 出| 文物| 项目名称_jar | 项目名称.jar
回答by Vugluskr
Idea 8.1.3
想法 8.1.3
Jar is ok, since there is compiled output in 'output' directory (project/out/production//)
Jar 没问题,因为在 'output' 目录(project/out/production//)中有编译输出
I guess, you have to run 'make' before building jar
我想,你必须在构建 jar 之前运行“make”
for dependencies just check "show library" and choose what you want.
对于依赖项,只需检查“显示库”并选择您想要的。
回答by Gary Rowe
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.
您可能想看看 Maven ( http://maven.apache.org)。您可以将其用作应用程序的主要构建过程,也可以仅用于通过“编辑配置”对话框执行某些任务。在 Maven 中创建一个模块的 JAR 的过程是相当简单的,如果您希望它在一个自执行 JAR 中包含所有依赖项,这也是微不足道的。
If you've never used Maven before then you want to read Better Builds With Maven.
如果您以前从未使用过 Maven,那么您想阅读Better Builds With Maven。
回答by Brian
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 :-)
Ant和Maven被广泛使用。我更喜欢 Ant,我觉得它更轻量级,而且你的开发人员更有控制权。有人会认为这是它的缺点:-)
回答by jbakirov
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.
可能有点晚了,但我设法通过这种方式解决了它-> 用 winrar 打开并删除 META-INF 文件夹中的 ECLIPSEF.RSA 和 ECLIPSEF.SF,此外,放置“Main-class: main_class_name”(不带“.class”) ) 在 MANIFEST.MF 中。确保在最后一行之后按了两次“Enter”,否则将不起作用。
回答by user5442490
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:
如何.jar
使用 IntelliJ IDEA 14.1.5创建一个:
- File > Save All.
- Run driver or class with main method.
- File > Project Structure.
- Select Tab "Artifacts".
- Click green plus button near top of window.
- Select JAR from Add drop down menu. Select "From modules with dependencies"
- Select main class.
- The radio button should be selecting "extract to the target JAR." Press OK.
- Check the box "Build on make"
- Press apply and OK.
- From the main menu, select the build dropdown.
- Select the option build artifacts.
- 文件 > 全部保存。
- 使用 main 方法运行驱动程序或类。
- 文件 > 项目结构。
- 选择选项卡“工件”。
- 单击窗口顶部附近的绿色加号按钮。
- 从添加下拉菜单中选择 JAR。选择“来自具有依赖项的模块”
- 选择主类。
- 单选按钮应选择“提取到目标 JAR”。按确定。
- 选中“在制造上构建”框
- 按应用和确定。
- 从主菜单中,选择构建下拉列表。
- 选择选项构建工件。
回答by fede beron
With Maven you can use this plugin:
使用 Maven,您可以使用此插件:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>[path you class main]</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
回答by Thilina Ashen Gamage
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
如果您在项目中使用第三方库,或者如果您在正确创建 MANIFEST.MF 文件时遇到问题,则在运行使用生成的 JAR 文件时可能会发生冲突
File > Project Structure > Artifacts > '+' > JAR > From modules with dependencies > .....
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!
相反,我建议您创建一个空的 JAR 并手动将所有其他元素添加到输出根目录。可以在此处找到有关此方法的精彩博客文章:http: //karthicraghupathi.com/2016/07/10/creating-an-executable-jar-in-intellij-idea/我尝试了那里提到的步骤,一切正常为了我!
回答by Crt
回答by Zsolt Tolvaly
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
这在 2017 年仍然是一个问题,我希望它会帮助那里的人!我发现了两种在 IntelliJ 2017.2 下创建工作 jar-s 的可能性
1. Creating artifact from IntelliJ:
1. 从 IntelliJ 创建工件:
- Go to project structure:
- 转到项目结构:
- Create a new artifact:
- 创建一个新工件:
- Select the main class, and be sure to change the manifest folder:
- 选择主类,并确保更改清单文件夹:
You have to change manifest directory:
您必须更改清单目录:
<project folder>\src\main\java
replace "java" with "resources"
用“资源”替换“java”
<project folder>\src\main\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.
然后你选择你想要打包在你的 jar 中的依赖项,或者 NEAR 你的 jar 文件
要构建您的工件,请转到构建工件并选择“重建”。它将使用您的 jar 文件及其依赖项创建一个“out”文件夹。
2. Using maven-assembly-plugin
2. 使用 maven-assembly-plugin
Add build section to the pom file
将构建部分添加到 pom 文件
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>ServiceCreate</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.svt.optimoo.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
- Create a new run/debug configuration:
- 创建一个新的运行/调试配置:
- Choose application:
- 选择应用:
- Fill in the form
- Add the "assembly:single" maven goal after build to be executed last
- 填写表格
- 在构建后添加“assembly:single”maven 目标最后执行
- Save it, then run
- 保存,然后运行
This procedure will create the jar file under the "target" folder
此过程将在“目标”文件夹下创建 jar 文件