Home > Software design >  How to run a main class from test sources with exec maven plugin?
How to run a main class from test sources with exec maven plugin?

Time:11-16

I have the following pom.xml configuration for exec maven plugin but it works only if the main class is in src/main/java, not if it's in src/test/java. I don't want this main class to end up in the distribution/release.

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <id>myexec</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>java</executable>
              <classpathScope>test</classpathScope>
              <arguments>
                <argument>-classpath</argument>
                <classpath/>
                <argument>com.mypackage.SomeMainClass</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>

The problem here is I am trying to run a class before it has been compiled (it gets compiled together with the test sources, after the generate-test-sources step). com.mypackage.SomeMainClass generates test sources *TestCase.java to be compiled and run.

So what could I do if I don't want the main class to end up in the released jar? I'd rather not develop a maven plugin just for this.

CodePudding user response:

The cleanest solution is probably to treat this class as its own external executable and thus build it as a separate module. That would involve turning the project into a Maven multi-module project if it isn't one already.

https://www.mojohaus.org/exec-maven-plugin/examples/example-exec-using-plugin-dependencies.html

The quick and dirty solution however... Maven allows you to set exclusions. If you don't want something to end up in the release jar, filter it out. I.E. :

excluding .class file from jar maven dependency

  • Related