Java 在移动到maven之后,我编译的jar文件不会';跑不动

Java 在移动到maven之后,我编译的jar文件不会';跑不动,java,maven,Java,Maven,实际上我花了很长时间才注意到。当我在netbeans中运行该项目时,它的工作方式与预期的一样。但是,当我执行构建时,我得到的PROJECTNAME-1.0-SNAPSHOT.jar文件没有任何作用。打开包装后,它看起来如下所示: 这是可疑的,在Maven之前,我编译的文件如下所示: Main类不应该在jar文件根目录中吗?JVM如何知道运行哪个主类 MANIFEST.MF也有一些令人不安的地方: 旧舱单 新舱单 似乎缺少主类项…用于使您的jar可执行。对于我来说,实际代码如下: <p

实际上我花了很长时间才注意到。当我在netbeans中运行该项目时,它的工作方式与预期的一样。但是,当我执行
构建
时,我得到的
PROJECTNAME-1.0-SNAPSHOT.jar
文件没有任何作用。打开包装后,它看起来如下所示:

这是可疑的,在Maven之前,我编译的文件如下所示:

Main
类不应该在jar文件根目录中吗?JVM如何知道运行哪个主类

MANIFEST.MF也有一些令人不安的地方:

旧舱单 新舱单 似乎缺少
主类
项…

用于使您的
jar
可执行。对于我来说,实际代码如下:

 <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>my.main.class</mainClass>
            </manifest>
          </archive>
          <outputDirectory>${project.build.directory}/result</outputDirectory>
        </configuration>
      </plugin>

org.apache.maven.plugins
:


org.codehaus.mojo
execmaven插件
1.3.2
我的主课

遗憾的是,许多IDE提供了一种简单的机制,可以将所有库jar打包到主构建jar中。这既不受欢迎,也可能对您的可部署库造成潜在的危害——您应该单独部署所有使用过的库,并使用类路径将它们连接起来

当Maven试图以最简单的方式正确操作时,它被设计为默认构建jar,只包含代码,而不包含其他内容。如果你能坚持这个解决方案,那么这样做可能是值得的

然而,有时有必要做到这一点,可能是为了在切换到Maven时复制现有的构建结果,也可能是因为不必部署几十个库,部署就足以成为一场噩梦。为了实现这一点,您可以使用Maven Shade插件构建Maven社区所称的Uber Jar

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.6</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <minimizeJar>true</minimizeJar>
          <filters>
            <filter>
              <!-- Make sure jaxb is included. -->
              <artifact>com.sun.xml.bind:jaxb-impl</artifact>
              <includes>
                <include>*.*</include>
              </includes>
            </filter> 
            <filter>
              <!-- Make sure jtds is included. -->
              <artifact>net.sourceforge.jtds:jtds</artifact>
              <includes>
                <include>**</include>
              </includes>
            </filter> 
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
                <exclude>META-INF/*.sf</exclude>
                <exclude>META-INF/*.dsa</exclude>
                <exclude>META-INF/*.rsa</exclude>
              </excludes>
            </filter>
          </filters>
        </configuration>
      </execution>
    </executions>
  </plugin>

org.apache.maven.plugins

MKYong撰写的一篇有趣的博客,介绍了使用
assembly/shade/one-jar

的利弊。您的项目是否使用了正确的maven布局?Java文件属于
src/main/Java
,资源属于
src/main/resources
。好吧,不完全是这样。我把资源放在
resources/
中,把java文件放在
src/
中。但我已经在我的配置中配置了这些路径。显然,您的配置有错误,否则,您的类将包含在您的JAR中。尝试应用默认的maven布局。@Alexander我的类始终在JAR中。我添加了这个,还添加了一些
maven程序集插件
,以绑定JAR文件中的依赖项。不过,双击my
.jar
没有任何效果。你知道我应该寻找什么,或者如何调试这个问题吗?结果是,由于默认配置文件丢失,我的程序另外引发了一个异常。现在它工作了,谢谢。换句话说,我不应该用捆绑的所有依赖项来构建我的jar?
maven shade插件
是否与我用来构建“Uber jar”的
maven汇编插件
冲突?@TomášZato-我不会走得太远,只是不推荐。不确定它会如何与程序集插件交互-如果与程序集插件相比,Maven Shade插件是创建fat/uber jar的更好的插件,因为它提供了类重新定位功能,以避免类路径中的类名冲突。
 <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>my.main.class</mainClass>
            </manifest>
          </archive>
          <outputDirectory>${project.build.directory}/result</outputDirectory>
        </configuration>
      </plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
      <mainClass>my.main.class</mainClass>
      <!--
      <commandlineArgs>-d 5409  -c 467 -t 2  -dlg true</commandlineArgs>
      -->
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.6</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <minimizeJar>true</minimizeJar>
          <filters>
            <filter>
              <!-- Make sure jaxb is included. -->
              <artifact>com.sun.xml.bind:jaxb-impl</artifact>
              <includes>
                <include>*.*</include>
              </includes>
            </filter> 
            <filter>
              <!-- Make sure jtds is included. -->
              <artifact>net.sourceforge.jtds:jtds</artifact>
              <includes>
                <include>**</include>
              </includes>
            </filter> 
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
                <exclude>META-INF/*.sf</exclude>
                <exclude>META-INF/*.dsa</exclude>
                <exclude>META-INF/*.rsa</exclude>
              </excludes>
            </filter>
          </filters>
        </configuration>
      </execution>
    </executions>
  </plugin>