0

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

Alex Man
  • 4,746
  • 17
  • 93
  • 178

1 Answers1

1

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>
Essex Boy
  • 7,565
  • 2
  • 21
  • 24
  • Thanks for reply, but in which `pom.xml` I will define the modules ` test1 test2 ` – Alex Man Jun 16 '17 at 06:59
  • You need too look at maven multi module projects. Your have a parent, then 2 children. Each child is in it's own directory. When the parent is built it will build each of it's children, in your case each child will have an executable jar in its target folder. – Essex Boy Jun 16 '17 at 07:12
  • but for one class I need o create a module project.....if I am having 10 classes....then 10 module projects I need to create.....Is there any other solution – Alex Man Jun 16 '17 at 07:44
  • Essex can we do using `maven-assembly-plugin` and `onejar-maven-plugin` – Alex Man Jun 16 '17 at 08:38
  • This is the way I would do it, I think you need to look at your question again, explain why you want all these main classes. – Essex Boy Jun 16 '17 at 09:20
  • I have only one main function class which is `ClassExecuter.java` which is having the implementation, `Test1.java` and `Test2.java` classes does not have main methods but have some common method signature lets say `start()` and this function is called within the `ClassExecuter` class using reflection – Alex Man Jun 16 '17 at 10:19