3

How do I tell Maven, what method/class to run and how do I tell it, what classes it should import and where?

I have to use the Eclipse Maven Plugin and I dont quite understand, how to input the commands. Do I alter the pom-file or are there any buttons for it or do I use the command line.

I`ve tried to find information about it, but because I have to use the eclipse Maven tool I seem to not find what im looking for.

My file structure

|
|- src
|   |
|    - serie04 
|        |
|         - callee
|             |
|              - Callee.java
|         - caller
|             |
|              - Caller.java
|- classes
|- jars

My two Java classes:

package serie04.callee;

public class Callee {
    public void callMe() {
        System.out.println("Callee says: You called me!");
   }
}
package serie04.caller;

import serie04.callee.Callee;

public class Caller {
    public static void main(String[] args) {
        Callee callee = new Callee();
        callee.callMe();
    }
}

I want that the a jar is created in the jar-File and that it prints "Callee says: You called me!" on the command line.

Burning_Beef
  • 73
  • 1
  • 6

1 Answers1

3

When using Maven, you are better off sticking to defaults- in example, the source code goes under src/main/java.

You could start a new Maven project from scratch. Using Eclipse menus, go to File > New > Other.. and then select Maven>Maven Project; click Next. In the next page, check the Create simple project combobox and click next. Use "sample" and "sample" as groupId and artifactId; then click Finish.

You now have a "sample" project in Eclipse; this project is Maven-based. Put your code under src/main/java. If you copy/paste the sources from Windows, you will then need to Refresh your project.

Now the project can be compiled from the command line using mvn package (try it..) and a jar file is produced by the build, but you need the manifest (needed to run java -jar ..) .


To add the manifest, have a look at this answer: How can I create an executable JAR..

After editing, your pom.xml will look like the one below. Build using mvn package from the cmd line. Run with java -jar sample-0.0.1-SNAPSHOT-jar-with-dependencies.jar.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>sample</groupId>
  <artifactId>sample</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>serie04.caller.Caller</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Edit The location for the sources can be customized, using the build.sourceDirectory as below

..
  <build>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
    <plugins>
      <plugin>
      ....
..
Daniele
  • 2,672
  • 1
  • 14
  • 20
  • Okay, thanks for the answer, but I have to use the directory as I wrote in my question. How do I tell Maven to use my path instead of the defaults? – Burning_Beef May 10 '19 at 21:19
  • 1
    If you really need to, you can specify the `sourceDirectory`; see the update. – Daniele May 10 '19 at 21:29
  • Okay, thanks again. However I must not create a new project. I am only allowed to create a buildscript. – Burning_Beef May 10 '19 at 21:36
  • uhm, if your build script uses Maven, you need a `pom.xml` file anyway- you can take the `pom.xml` and place it in your main project folder. The build script itself will just call `mvn clean package` . – Daniele May 10 '19 at 21:39