I have written a console app (.java class with main function), and I'd like to call this main (or, for example some static method before execution of my big project) how to do this with Maven (script?)?
-
your question is unclear. Maven is used to manage dependencies and build processing – jhamon Oct 15 '14 at 12:10
-
something like this : http://stackoverflow.com/questions/25352751/maven-run-a-java-method-or-script-before-war-is-about-to-be-built But for method from the class - I need to build custom xml – curiousity Oct 15 '14 at 12:13
-
will you call it main function, or it will be your main method? if first one im not sure can you do it in java at all – user902383 Oct 15 '14 at 12:13
-
let it be main method - I could do so or so – curiousity Oct 15 '14 at 12:14
2 Answers
go and look at the exec maven plugin. you can use it to execute either a native executable, a script, or a java main() at any point in the build. like so:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
you cannot run just any java method you want, but writing a small class with a main() method that invokes whatever you want is pretty easy.
another gotcha is that the class you want to run has to be compiled before you run it. this means that either this execution has to happen after the compile phase or you need to split your project into 2 modules - one containing the main class and the other containing the rest of you code (that will depend on the 1st module to get proper build order)
if you still insist on arbitrary code, there's always the groovy maven plugin, which allows you to write groovy code inline to be executed during the build.
-
wow! cool.. thank you a lot - I will try to implement it now for my prj. – curiousity Oct 15 '14 at 12:16
-
-
still error(( I am trying to run this piece of code with mvn exec:exec but I ll get Inside the definition for plugin 'exec-maven-plugin' specify the following:
... -OR- on the command line, specify: '-Dexec.executable=VALUE' – curiousity Oct 15 '14 at 13:19VALUE -
@curiousity Did you follow the first link radai gave? There's a link to the [exec:exec](http://mojo.codehaus.org/exec-maven-plugin/exec-mojo.html) goal at the top. And there: **Required Parameters** - **executable**. This parameter is probably missing in your POM. – Gerold Broser Oct 15 '14 at 23:35
As an alternative to the exec-maven-plugin
you can also use the Apache Maven Scripting Plugin. It can execute scripts in any JSR223
compatible scripting language.
For references on the scripting library options you have, see the following questions:
- Java ScriptEngine supported languages
- Where can I find a list of available JSR-223 scripting languages?
I have written a more detailed answer on how this plugin works in the following answer:
For your needs on executing any piece of Java code from the current project, you can use the following plugin setup:
<!-- Scripting Plugin: run ScriptEngine compatible scripts -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scripting-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>run-script</id>
<phase>compile</phase>
<goals>
<goal>eval</goal>
</goals>
</execution>
</executions>
<configuration>
<engineName>beanshell</engineName>
<!-- <scriptFile>use this when possible; no code in the POM is preferred</scriptFile> -->
<script>
log.info("Hello from " + project.getArtifact());
// run code from the current project
// new my.package.MyClass().myMethodToExecute();
</script>
</configuration>
<dependencies>
<!-- Script Engine: Beanshell -->
<dependency>
<groupId>org.apache-extras.beanshell</groupId>
<artifactId>bsh</artifactId>
<version>2.0b6</version>
</dependency>
<!-- Dependency to current project (self) for executing code -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
You can use any id
you like in the execution
element.
The phase
needs to be compile
or later, otherwise you can't use the code from your current project. If you need to execute it earlier, you could put this code in a different artifact or module and depend on that.
By changing the dependency
and engineName
you can use any other compatible scripting language. You'll need to pick a Java-like language that is capable of calling & executing Java code.
This gives the following example output in the maven build log:
[INFO] --- maven-scripting-plugin:3.0.0:eval (run-script) @ tests ---
[INFO] Hello from local:tests:jar:1.0
You can of course remove the "Hello world" and just execute the code you want.

- 1,091
- 2
- 11
- 18