繁体   English   中英

一罐,一罐,主要罐子来自于依赖罐

[英]one jar with one class and main class from dependecy jar

我在一个项目中有两个名为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>

有人可以帮我吗

您需要有一个带有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>

然后,您有2个项目test1和test2项目,它们将生成jar,使用阴影插件使其执行主类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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM