我有一个项目,包含一个单独的模块,和一些依赖。 我想在一个单独的目录中创建一个JAR,其中包含编译后的模块。此外,我希望在我的模块旁边有依赖项。

无论我如何扭曲IntelliJ的“构建JAR”过程,我的模块的输出都是空的(除了一个META-INF文件)。


当前回答

使用

mvn clean package spring-boot:repackage

在IntelliJ终端中输入以上命令。它生成一个带有Executable Jar的目标文件。

其他回答

如果你试图用kotlin构建一个jar,你需要创建一个src/main/java文件夹,并使用这个文件夹作为META-INF文件夹的位置。

这在2017年仍然是一个问题,我希望它能帮助到那里的人! 我发现在IntelliJ 2017.2下创建工作罐有2种可能性

1. 从IntelliJ创建工件:

进入项目结构:

创建一个新的工件:

选择主类,并确保更改manifest文件夹:

您必须更改清单目录:

<project folder>\src\main\java 

将“java”替换为“resources”

<project folder>\src\main\resources

它应该是这样的:

然后你选择你想要打包在jar或jar文件附近的依赖项 要构建您的工件,请前往构建工件并选择“重建”。它将创建一个包含jar文件及其依赖项的“out”文件夹。

2. 使用maven-assembly-plugin

向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>

创建新的运行/调试配置:

选择应用程序:

填写表格 在构建后最后执行“assembly:single”maven目标

保存它,然后运行

这个过程将在“目标”文件夹下创建jar文件

如果您正在阅读这个答案,因为您面临“资源文件未找到”错误,请尝试以下方法:

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.

当我使用这些解决方案这个错误来:

java -jar xxxxx.jar

在xxx.jar中没有主清单属性

解决方案是:

您必须更改清单目录:

<project folder>\src\main\java 

将Java更改为资源

<project folder>\src\main\resources

下面是如何使用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