2

I am starting to learn Maven by reading https://spring.io/guides/gs/maven/.

In the examples, after running mvn compile successfully, how can I run the program via maven? This part seems missing from the article.

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590
  • Are you using Eclipse? – Akshay Jan 08 '18 at 05:40
  • 1
    `mvn test` should run your tests. You can try `mvn jar:jar`. You are reading the "getting started" guide, other guides include other options. [Introduction to the Build Lifecycle](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html) – Elliott Frisch Jan 08 '18 at 05:41
  • @Akshay No for this example and link. – Tim Jan 08 '18 at 05:45

3 Answers3

2

You can invoke a Java program (i.e. with a public static void main(String[] args) signature) with the classpath of the combined dependencies for the current pom.xml using

mvn -q exec:java

You need to configure the main class to invoke in your pom.xml similar to

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>demo.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

This is useful for testing and development, but not deployment

See http://www.mojohaus.org/exec-maven-plugin/usage.html for full details.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Thanks. If the execution of a program needs some dependencies, will Maven or `exec-maven-plugin` reuse the dependencies used in compilation in execution, or have to specify the dependencies used in compilation again in execution? – Tim Jan 24 '18 at 17:22
  • I believe the `runtime` Maven scope is what being used. This is slightly different from the `compilation` scope used when compiling. If you do not use scopes, the dependencies used when compiling are on your classpath when running. – Thorbjørn Ravn Andersen Jan 24 '18 at 18:57
1

Generally, maven is not used for running code. This is a build tool that you can use for compiling, running unit or integration tests, deploying you your code locally and remotely, etc..

It is based around the idea of a build lifecycle where which is in its turn is defined by a list of build phases. For example, the default lifecycle has the following phases:

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • verify - run any checks on results of integration tests to ensure quality criteria are met
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

For more information you can refer to this.

UPDATE:

Having said that, it is possible as mentioned in Thorbjørn Ravn Andersen answer here.

Eugene S
  • 6,709
  • 8
  • 57
  • 91
  • You know of `mvn exec:java`? – Thorbjørn Ravn Andersen Jan 08 '18 at 07:00
  • @ThorbjørnRavnAndersen Yes, but that's an external plugin, isn't it? – Eugene S Jan 08 '18 at 07:02
  • It is a maven plugin which does what is needed here. – Thorbjørn Ravn Andersen Jan 08 '18 at 07:07
  • Spring boot will also run similarly. Of course they're all additional plugins, but if you're working with just what you get out of the box, you're not going to be doing too many useful things with Maven. – Kayaman Jan 08 '18 at 07:36
  • @Kayaman I have nothing against "external" plugins. Just seemed to me that the question was about classic(if there is such thing) usage. – Eugene S Jan 08 '18 at 07:44
  • Thanks. If execution of a program needs some dependencies, can packaging solve the problem by including those dependencies in the same jar file as the program's own .class file? – Tim Jan 24 '18 at 17:23
  • @Tim You can do this using the maven-assembly plugin with the "jar-with-dependencies" descriptor. And there's the shade maven plugin. It can be used to package and rename dependencies (to omit dependency problems on the classpath). – Eugene S Jan 25 '18 at 01:52
  • https://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven – Eugene S Jan 25 '18 at 01:52
1

The Maven build process has a number of lifecycles, or points in the process. compile is one of the build steps, but most likely running the following would resolve your issue:

mvn clean package

This would generate a JAR file, in the folder where you ran it. You can then try running this JAR file using java.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Wouldn't `mvn install` just install(copy the compiled code) the package to local repo? – Eugene S Jan 08 '18 at 05:43
  • @EugeneS `clean install` does generate a JAR output, at least when I use it it does. – Tim Biegeleisen Jan 08 '18 at 05:44
  • But does generating JAR output means running the code? – Eugene S Jan 08 '18 at 05:45
  • @EugeneS Did you read the question? The OP wants to know how to run code compiled my Maven. My answer says to do `install`, generate a JAR, and then run that JAR. Do you take issue with this? – Tim Biegeleisen Jan 08 '18 at 05:47
  • The question was "how I run the program via Maven". Your answer is correct of course in general but I think you made few assumptions in regards to what he meant. To me it reads as if he wanted to actually run the program using Maven. I might be wrong of course. – Eugene S Jan 08 '18 at 05:49
  • 1
    `mvn package` will do what you describe. `mvn install` is a next step in maven lifecycle that will copy the generated artifact (jar, war or whatever) in the folder where your repositories are. – Luiggi Mendoza Jan 08 '18 at 05:56