我有一个项目,包含一个单独的模块,和一些依赖。 我想在一个单独的目录中创建一个JAR,其中包含编译后的模块。此外,我希望在我的模块旁边有依赖项。
无论我如何扭曲IntelliJ的“构建JAR”过程,我的模块的输出都是空的(除了一个META-INF文件)。
我有一个项目,包含一个单独的模块,和一些依赖。 我想在一个单独的目录中创建一个JAR,其中包含编译后的模块。此外,我希望在我的模块旁边有依赖项。
无论我如何扭曲IntelliJ的“构建JAR”过程,我的模块的输出都是空的(除了一个META-INF文件)。
当前回答
以下是IntelliJ IDEA 2018.3 Help的官方解答。我试过了,成功了。
要从模块构建JAR文件; 在主菜单上,选择Build | Build Artifact。 从下拉列表中,选择JAR类型的所需工件。 该列表显示为当前项目配置的所有工件。要构建所有已配置的构件,请选择Build all 构件的选择。
其他回答
当我使用这些解决方案这个错误来:
java -jar xxxxx.jar
在xxx.jar中没有主清单属性
解决方案是:
您必须更改清单目录:
<project folder>\src\main\java
将Java更改为资源
<project folder>\src\main\resources
Ant和Maven被广泛使用。我更喜欢Ant,我觉得它更轻量级,你作为开发人员更有控制权。有些人会认为这是它的缺点:-)
在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>
如果您正在阅读这个答案,因为您面临“资源文件未找到”错误,请尝试以下方法:
File -> Project Structure -> Artiface -> New, type is Other. Give it a nice name, and in the Output Layout, press Create Archive to create a new jar, and again, give it a nice name ;) Click on the jar you just added, add your compiled module output in the jar. Click on the jar again, in the lower pane, select your project path and add Manifest File then set correct Main Class and Class Path. Click Add Library Files, and select libraries you need (you can use Ctrl+A to select all). Build -> Build Artifact -> Clean & Build and see if the error is gone.
下面是如何使用IntelliJ 10 http://blogs.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/构建一个罐子
文件->项目结构->项目设置->工件->点击绿色加号-> Jar ->从模块依赖…
如果您需要使jar可运行,请选择一个主类(带有Main()方法的类)。
上面设置了“骨架”,即罐子将保存到的位置。要实际构建并保存它,请执行以下操作:
提取到目标Jar
OK
构建|构建Artifact |构建
尝试从。jar文件中提取
ProjectName | out | artifacts | ProjectName_jar | ProjectName.jar