Java 从 intellij 中的 Maven 项目创建一个 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30204884/
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 09:19:08  来源:igfitidea点击:

Creating a jar from a maven project in intellij

javamavenintellij-idea

提问by Joe

I created a new maven project in IntelliJ and set packaging to jar but when I build it, the target folder does not contain a jar. I'm sure its something really dumb on my part but there are just so many different things i'm reading on different websites and I just feel better asking. enter image description here

我在 IntelliJ 中创建了一个新的 maven 项目并将打包设置为 jar 但是当我构建它时,目标文件夹不包含 jar。我确信这对我来说真的很愚蠢,但我在不同的网站上阅读了很多不同的东西,我只是觉得问得更好。在此处输入图片说明

回答by sethu

You need the maven jar plugin in order to create a jar

您需要 maven jar 插件才能创建 jar

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>add your main class</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

https://maven.apache.org/plugins/maven-jar-plugin/

https://maven.apache.org/plugins/maven-jar-plugin/

回答by kiwiron

Assuming that the screen-shot shows the complete pom file, you are missing the entries that define the artifact. Try adding something like this following immediately after the tag:

假设屏幕截图显示了完整的 pom 文件,您将缺少定义工件的条目。尝试在标签后立即添加如下内容:

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.example</groupId>
  <artifactId>stackoverflow-question</artifactId>
  <version>0.0.1-SNAPSHOT</version>

You should end up with stackoverflow-question-0.0.1-SNAPSHOT.jar in your /target directory. You may need to refresh the directory to see it (You certainly have to in Eclipse)

您应该在 /target 目录中得到 stackoverflow-question-0.0.1-SNAPSHOT.jar。您可能需要刷新目录才能看到它(您当然必须在 Eclipse 中)

回答by arghtype

You should build you project using IDEA's Maven Projectsview.

您应该使用 IDEA 的Maven Projects视图构建您的项目。

View -> Tool Windows -> Maven Projects

查看 -> 工具窗口 -> Maven 项目

or open it from left bottom corner menu:

或从左下角菜单打开它:

menu

菜单

And then build your project with maven goals - i.e. package: maven project

然后使用 maven 目标构建您的项目 - 即包: Maven 项目

If packaging is set to jarin pom.xml, you will get a jar in targetdir.

如果 Packaging 设置为jarin pom.xml,您将在targetdir 中得到一个 jar 。

回答by ankit

Go to the maven project and double click clean and package.

进入maven项目,双击clean and package。

For just do following:

只需执行以下操作:

enter image description here

在此处输入图片说明

回答by Alec

You need to have "jar" in packaging section of your pom.xml. You can add this before dependencies section:

您需要在 pom.xml 的包装部分中有“jar”。您可以在依赖项部分之前添加它:

<packaging>**jar**</packaging>

Then go to View -> Tool Windows -> Maven and in Maven window expand everything and double click on "package" in Lifecycle section.

然后转到 View -> Tool Windows -> Maven 并在 Maven 窗口中展开所有内容并双击 Lifecycle 部分中的“package”。