Skip to content

Instantly share code, notes, and snippets.

@jstaursky
Last active April 17, 2021 19:18
Show Gist options
  • Save jstaursky/03986e0c1ad24ccec123b8c38538776a to your computer and use it in GitHub Desktop.
Save jstaursky/03986e0c1ad24ccec123b8c38538776a to your computer and use it in GitHub Desktop.

Simplest java maven hello-world style project.

  1. create the directory structure

mkdir -p tutorial/src/main/java/hello then touch tutorial/src/main/java/hello/Main.java

  1. put in the java file tutorial/src/main/java/hello/Main.java
package hello;
public class Main {
  public static void main(String[] args) {
    System.out.println("Hello world");
  }
}
  1. create pom.xml at the project root (under the tutorial directory)
<project>
 <modelVersion>4.0.0</modelVersion>
 <groupId>hello</groupId>
 <artifactId>java-archive</artifactId>
 <version>1.0-SNAPSHOT</version>

<properties>
     <maven.compiler.source>1.8</maven.compiler.source>
     <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
  <plugins>
    <plugin>
      <!-- Build an executable JAR -->
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>hello.Main</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

</project>
  1. execute mvn package to create an executable jar file and enter java -jar <generated-jar-file>

refs: https://cwiki.apache.org/confluence/display/MAVEN/Tutorial%3A+Build+a+JAR+file+with+Maven+in+5+minutes https://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute https://stackoverflow.com/questions/53034953/error-source-option-5-is-no-longer-supported-use-6-or-later-on-maven-compile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment