Your Way to Maven

Software Development
Published May 27, 2022 ยท less than a minute read
In this article, I'll be listing all the important notes and maven scripts that you'll probably need while developing in Java. It is more of an FAQ page.

How to compile a maven project?

mvn clean install -s settings.xml

How to compile in parallel threads?

mvn -T {number_of_threads} clean install

How to generate a runnable JAR?

Add this to your pom.xml file

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>{your_class_full_name}</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

How to generate one JAR including all dependencies?

In the pom.xml file:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>make-assembly</id> 
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>{your_class_full_name}</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <finalName>{output_jar_name}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

How to exclude tests from the compilation process?

mvn clean install -DskipTests -Dmaven.test.skip=true -Dmaven.test.skip.exec

How to run a main method from the command line?

mvn compile exec:java -D"exec.mainClass"="{your_class_full_name}"