IntelliJ:Maven:将Artifact构建到Jar但不能执行jar

时间:2016-05-06 19:19:52

标签: java maven intellij-idea build artifact

我已经设置了一个带有mavens标准目录结构的项目:

应用:      - > src - > main - > java - > com - > company - > appname - > //这里的类

我还在java目录下有一个带有MANIFEST.MF的META-INF目录:

 Manifest-Version: 1.0
 Main-Class: com.company.appname.MyMain

我的maven pom.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.dsds.cryptoapp</groupId>
<artifactId>Crytpo</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.jgoodies</groupId>
        <artifactId>jgoodies-forms</artifactId>
        <version>1.9.0</version>
    </dependency>
</dependencies>

如您所见,我将JGoodiesLibrary导入为项目的依赖项。

到目前为止一切正常。

现在我通过Maven通过clean在IntelliJ中构建一个工件 - &gt;编译 - &gt;包 - &gt;安装。

创建了jar,但是我的库丢失了。 此外,当我通过带有“java -jar myapp.jar”的Windows cmd启动jar时,它表示我的mainmanifestattribute缺失,即使它已经存在!

2 个答案:

答案 0 :(得分:2)

Jar插件可以为您生成清单,我建议以这种方式使用它:https://maven.apache.org/shared/maven-archiver/examples/classpath.html#Make_The_Jar_Executable

我想你必须将classpath添加到清单文件中:https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

答案 1 :(得分:1)

您是否已验证未包含的库是否已添加到项目的包中?您可以转到项目结构(Alt Ctrl Shift S),输出布局中的工件,在最终包中添加所需的库。

相关问题