Running main method from test class via maven -
i have following in pom.xml:
<build> ... <plugins> ... <plugin> <groupid>org.codehaus.mojo</groupid> <artifactid>exec-maven-plugin</artifactid> <version>1.2.1</version> <configuration> <mainclass>com.myorg.myclass</mainclass> </configuration> </plugin> </plugins> </build>
the class com.myorg.myclass
in test source directory, , able run following command:
mvn -e exec:java -dexec.classpathscope="test"
i want to:
- avoid having use
-dexec.classpathscope="test"
, cannot work out how configure plugin in test classpath. - write more plugins other classes (which each have different configurations), right can run
exec:java
. there way label plugin call via label rather "run whatever in exec:java"? - pull in
-javaagent
property. have property defined in pom.xml, being used test cases. "custom" plugin pick these properties or need pull them in?
here properties, defined directly under <project>
.
<properties> <spring.version>3.2.6.release</spring.version> <atomikos.version>3.9.2</atomikos.version> <loadtimeweaverargline>-javaagent:"${settings.localrepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</loadtimeweaverargline> </properties>
attempt profile
following on @michal's suggestion (https://stackoverflow.com/a/30839824/257233) have tried:
<profile> <id>run-importer</id> <properties> <loadtimeweaverargline>-javaagent:"${settings.localrepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</loadtimeweaverargline> </properties> <build> <plugins> <plugin> <groupid>org.codehaus.mojo</groupid> <artifactid>exec-maven-plugin</artifactid> <version>1.2.1</version> <configuration> <executable>java</executable> <!-- none of these 3 options work. <commandlineargs>-javaagent:c:/users/robbram/.m2/repository/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar</commandlineargs> <commandlineargs>${loadtimeweaverargline}</commandlineargs> <commandlineargs>-javaagent:"${settings.localrepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</commandlineargs> <argline>${loadtimeweaverargline}</argline> --> <classpathscope>test</classpathscope> <mainclass>com.myorg.myclass</mainclass> </configuration> </plugin> </plugins> </build> </profile>
i run with:
mvn -e exec:java -prun-importer
i getting following exception either of options:
[error] failed execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project toltat-model: exception occured while executing java class. null: invocationtargetexception: error creating bean name 'loadtimeweaver' defined in class org.springframework.context.annotation.loadtimeweavingconfiguration: instantiation of bean failed; nested exception org.springframework.beans.factory.beandefinitionstoreexception: factory method [public org.springframework.instrument.classloading.loadtimeweaver org.springframework.context.annotation.loadtimeweavingconfiguration.loadtimeweaver()] threw exception; nested exception java.lang.illegalstateexception: classloader [java.net.urlclassloader] not provide 'addtransformer(classfiletransformer)' method. specify custom loadtimeweaver or start java virtual machine spring's agent: -javaagent:org.springframework.instrument.jar -> [help 1] org.apache.maven.lifecycle.lifecycleexecutionexception: failed execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project toltat-model: exception occured while executing java class. null @ org.apache.maven.lifecycle.internal.mojoexecutor.execute(mojoexecutor.java:216) @ org.apache.maven.lifecycle.internal.mojoexecutor.execute(mojoexecutor.java:153) @ org.apache.maven.lifecycle.internal.mojoexecutor.execute(mojoexecutor.java:145)
i can confirm main class com.myorg.myclass
being executed. can see other output it. can confirm loadtimeweaverargline
works in other parts of pom. used integration tests:
<profile> <id>integration-tests</id> <build> <plugins> <!-- integration tests require additional loadtime spring argument --> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-surefire-plugin</artifactid> <version>2.16</version> <configuration> <forkmode>once</forkmode> <argline> ${loadtimeweaverargline}</argline> <skip>false</skip> </configuration> </plugin> </plugins> </build> </profile>
attempt 2 profile , mvn exec:exec
upodate (wednesday, 17th of june 2015, 12:11:17 pm): following on michal's latest comment have working want:
<profile> <id>run-importer</id> <properties> <loadtimeweaverarg>-javaagent:"${settings.localrepository}/org/springframework/spring-agent/2.5.6/spring-agent-2.5.6.jar"</loadtimeweaverarg> <log4jconfigarg>-dlog4j.configuration=file:${project.build.directory}/path/to/log4j.properties</log4jconfigarg> <mainclassarg>com.myorg.myclass</mainclassarg> <arg1>foo</arg1> </properties> <build> <plugins> <plugin> <groupid>org.codehaus.mojo</groupid> <artifactid>exec-maven-plugin</artifactid> <version>1.2.1</version> <goals> <goal>exec</goal> </goals> <configuration> <executable>java</executable> <classpathscope>test</classpathscope> <arguments> <argument>${log4jconfigarg}</argument> <argument>${loadtimeweaverarg}</argument> <argument>-classpath</argument> <classpath /> <argument>${mainclassarg}</argument> <argument>${arg1}</argument> </arguments> </configuration> </plugin> </plugins> </build> </profile>
and run with:
mvn -e exec:exec -prun-importer
the advantages of approach:
- the whole purpose of profile run "special code" should never deployed needs make use of code in src , test src.
- i note michal's suggestion deserves module itself. if code base wasn't established (large, old, complicated), consider this.
- it leaves room in case ever needs duplicated, there no "competition" regarding gets run
mvn -e exec:exec
. - i can specify java agent, log4j , lots of other config using variables exist in pom.
- i can override of these arguments on command line
-darg1="bar"
i'm not sure you're going accomplish since it's quite stilted use of maven. however, stuff technically possible:
1/
<plugin> <groupid>org.codehaus.mojo</groupid> <artifactid>exec-maven-plugin</artifactid> <version>1.2.1</version> <configuration> <mainclass>com.myorg.myclass</mainclass> <classpathscope>test</classpathscope> </configuration> </plugin>
2/
you can use profiles , put different plugin configurations in different profile. call:
mvn -e exec:java -pmy-first-profile
it's weird however, think. recommendation here reconsider case , maybe pick way or tool that.
3/
final solution in question after author's edit.
Comments
Post a Comment