8.9

Java, Maven

make things

We will use Java 21 for our projects and Maven for compiling source files, managing dependencies, and assembling executables.

Using Maven with IntelliJ When you create your project, select Maven as the Build system. IntelliJ will generate a file named pom.xml in the project. The pom file specifies the settings and information used by maven to compile, test, and package your project. Initially, it will look like this:

    <?xml version="1.0" encoding="UTF-8"?>

    <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>org.example</groupId>

        <artifactId>SampleMavenApp</artifactId>

        <version>1.0-SNAPSHOT</version>

    

        <properties>

            <maven.compiler.source>21</maven.compiler.source>

            <maven.compiler.target>21</maven.compiler.target>

            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        </properties>

    

    </project>

For this project, edit the groupId to be com.neu.halp; all of the Java you write in this class should be in the com.neu.halp package, or a subpackage.

Adding Dependencies You may also use the "New Dependency" option in IntelliJ’s "Generate" wizard. To use an external library with your project, such as JUnit, it must be specified in your pom. To do this, we must specify the library and its version in a dependencies list:

    <dependency>

       <groupId>junit</groupId>

       <artifactId>junit</artifactId>

       <version>3.8.1</version>

    </dependency>

When we edit the pom in the IDE, we must tell IntelliJ to take note: You can also use the "Reload All Maven Projects" button in the Maven tool window.

load Maven changes

Building an Executable To build an executable JAR, we will use the Maven assembly plugin. Instructions for the plugin go in the build section of the pom. The example below calls for the creation of a JAR named MyExecutable.jar. When we run java -jar MyExecutable.jar, it invokes the main method of the MyMain class in the com.neu.halp package. Executing the package goal will attempt to assemble the executable.

    <build>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-assembly-plugin</artifactId>

                <version>2.2-beta-5</version>

                <executions>

                    <execution>

                        <id>MyExecutable</id>

                        <configuration>

                            <archive>

                                <manifest>

                                     <mainClass>com.neu.halp.MyMain</mainClass>

                                </manifest>

                            </archive>

                            <appendAssemblyId>false</appendAssemblyId>

                            <descriptorRefs>

                                <descriptorRef>jar-with-dependencies</descriptorRef>

                            </descriptorRefs>

                            <finalName>MyExecutable</finalName>

                        </configuration>

                        <phase>package</phase>

                        <goals>

                            <goal>single</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </build>