Run multiple classes using Maven

风格不统一 提交于 2019-12-13 07:52:31

问题


I have a package with several classes (each one encapsulating an executable program, i.e. with a main() method), i.e.:

com.myorg.examples.classA
com.myorg.examples.classB
etc.

All the classes belong to the same package (com.myorg.examples).

I know I can use maven to run one of such classes, eg:

mvn exec:java -D"exec.mainClass"="com.myorg.examples.classA"

I also know I can configure exec-maven-plugin in order to do the same using a shorter command, eg:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <executions>
    <execution>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>com.myorg.examples.classA</mainClass>
  </configuration>
</plugin>

then use:

mvn exec:java

However, I wonder if it would be possible:

  1. To use the exec-maven-plugin (or antoher one) to configure the several executions and do something like this

    mvn exec:classA       # or,
    mvn exec:java classA 
    

    so classA is run, but using a shorter syntax than plain exec:java. Looking to the XML structure it seems only one class can be set, so I'm not sure how to achieve that.

  2. To execute all the classes, in sequence, eg:

    mvn exec-all
    

    in order to run classA, next classB, and so on.

Any help or link about these topics will be highly welcomed. Thanks!

EDIT: second part of the question has been spined off to this other post.


回答1:


You can configure several executions which is available since Maven version 3.3.1

<project...>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            <id>default-cli</id>
            <configuration>
              <mainClass>com.soebes.test.First</mainClass>
            </configuration>
          </execution>
          <execution>
            <id>second-cli</id>
            <configuration>
              <mainClass>com.soebes.test.Second</mainClass>
            </configuration>
          </execution>
          <execution>
            <id>third-cli</id>
            <configuration>
              <mainClass>com.soebes.test.Third</mainClass>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins> 
  </build>
</project>

So you can now call Maven via:

The following will execute the one where id: default-cli:

mvn exec:java

The following will execute the one where id: second-cli:

mvn exec:java@second-cli

The following will execute the one where id: thirds-cli:

mvn exec:java@third-cli

The question is why you have several main classes in a different packages but within a single Maven module which sounds for me to have different modules (as you already have packages).. The other question is why do you need to execute them via exec-maven-plugin? What is the intention ?



来源:https://stackoverflow.com/questions/51504549/run-multiple-classes-using-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!