【问题标题】:one jar with one class and main class from dependecy jar一个 jar,一个类和依赖 jar 中的主类
【发布时间】:2017-06-16 06:18:38
【问题描述】:

我有一个项目,其中包含两个名为 Test1Test2 的类。

Test1Test2 都不是主类。现在我有一个名为 cloudexe.jar 的依赖项,它有一个主类 ClassExecuter。现在我的问题是我希望 ClassExecuter 作为 test1.jartest2.jar 的主类。

test1.jar 应该只包含 Test1 类及其所有依赖项,包括 cloudexe.jar 同样 test2.jar 应该只包含 Test2 类及其所有依赖项,包括 cloudexe.jar

现在当我打包 pom.xml 时,我得到了 test1.jar 和 test2.jar,但我得到如下所示

Exception in thread "main" java.lang.NoSuchMethodException: com.uber.Test1.main([Ljava.lang.String;)
        at java.lang.Class.getMethod(Class.java:1678)
        at com.simontuffs.onejar.Boot.run(Boot.java:339)
        at com.simontuffs.onejar.Boot.main(Boot.java:166)

我的 pom.xml 如下所示

<build>
  <plugins>
  <plugin>
    <groupId>com.jolira</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
      <execution>
        <id>build-first</id>
          <configuration>
            <mainClass>com.uber.Test1</mainClass>
            <attachToBuild>true</attachToBuild>
            <classifier>onejar</classifier>
            <filename>test1.jar</filename>
          </configuration>
          <goals>
            <goal>one-jar</goal>
          </goals>
        </execution>
      <execution>
        <id>build-second</id>
          <configuration>
            <mainClass>com.uber.Test2</mainClass>
            <attachToBuild>true</attachToBuild>
            <classifier>onejar</classifier>
            <filename>test2.jar</filename>
          </configuration>
          <goals>
            <goal>one-jar</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

<pluginRepositories>
  <pluginRepository>
     <id>onejar-maven-plugin.googlecode.com</id>
     <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
  </pluginRepository>
</pluginRepositories>

谁能帮我解决这个问题

【问题讨论】:

    标签: java eclipse maven maven-assembly-plugin onejar


    【解决方案1】:

    您需要有一个有 2 个子项目的父项目:

    <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>com.example</groupId>
      <artifactId>parent</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
    
      <modules>
        <module>test1</module>
        <module>test2</module>
      </modules>
    
    </project>
    

    然后你有两个项目 test1 和 test2 项目,它们会产生 jars,使用 shade 插件让它执行主类 ClassExecuter :

    <?xml version="1.0"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <modelVersion>4.0.0</modelVersion>
    
      <parent>
        <groupId>com.example</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
    
      <artifactId>test1</artifactId>
      <packaging>jar</packaging>
    
      <dependencies>
          ... all your dependencies including cloudexe.jar
      </dependencies>
    
    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.0.0</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <manifestEntries>
                                            <Main-Class>....ClassExecuter</Main-Class>
                                        </manifestEntries>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
            </plugins>
        </build>
    
    </project>
    

    【讨论】:

    • 感谢回复,不过pom.xml我会在其中定义模块&lt;modules&gt; &lt;module&gt;test1&lt;/module&gt; &lt;module&gt;test2&lt;/module&gt; &lt;/modules&gt;
    • 你也需要看看 maven 多模块项目。你有一个父母,然后是 2 个孩子。每个孩子都在它自己的目录中。构建父级时,它将构建它的每个子级,在您的情况下,每个子级在其目标文件夹中都有一个可执行 jar。
    • 但是对于一个类,我需要创建一个模块项目.....如果我有 10 个类....那么我需要创建 10 个模块项目.....有没有其他解决方案
    • 埃塞克斯我们可以使用maven-assembly-pluginonejar-maven-plugin
    • 这就是我会做的方式,我想你需要再看看你的问题,解释为什么你想要所有这些主类。
    猜你喜欢
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2014-01-08
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多