一个罐子,一个类和主类从依赖罐 [英] one jar with one class and main class from dependecy jar

查看:149
本文介绍了一个罐子,一个类和主类从依赖罐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目有两个类别,名为 Test1 Test2



Test1 Test2 不是主类。现在我有一个名为 cloudexe.jar 的依赖项,它具有一个主类 ClassExecuter 。现在我的问题是,我希望 ClassExecuter 作为 test1.jar test2.jar 的主要类。



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



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

 线程mainjava.lang中的异常。 NoSuchMethodException:com.uber.Test1.main([Ljava.lang.String;)
在java.lang.Class.getMethod(Class.java:1678)
在com.simontuffs.onejar.Boot。运行(Boot.java:339)
在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>
<执行>
< 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.0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi: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>父< / artifactId>
< version> 1.0-SNAPSHOT< / version>
< packaging> pom< / packaging>

< modules>
< module> test1< / module>
< module> test2< / module>
< / modules>

< / project>

然后你有2个项目test1和test2项目,他们将生产罐子,使用阴影流水它执行主类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.xsdxmlns = 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>父< / artifactId>
< version> 1.0-SNAPSHOT< / version>
< / parent>

< artifactId> test1< / artifactId>
< packaging> jar< / packaging>

<依赖关系>
...所有的依赖项,包括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>
<执行>
< execution>
< phase> package< / phase>
< goals>
< goal> shade< / goal>
< / goals>
< configuration>
<变压器>
< 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>


I am having one project with two classes named as Test1 and Test2.

Both Test1 and Test2 are not main classes. Now I have one dependency named cloudexe.jar which has a main class ClassExecuter. Now my issue is that I want ClassExecuter as the main class for both test1.jar and test2.jar.

test1.jar should contains only Test1 class and all its dependencies including cloudexe.jar similarly test2.jar should contains only Test2 class and all its dependencies including cloudexe.jar

Now when my package my pom.xml I gets test1.jar and test2.jar but I am getting like as shown below

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)

My pom.xml is given below

<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>

Can anyone please help me on this

解决方案

You need to have a parent with 2 child projects :

<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>

Then you have 2 projects test1 and test2 projects, they will produce jars, use the shade pluging to have it execute the main class 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>

这篇关于一个罐子,一个类和主类从依赖罐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆