Snippet Maven

With snippet Maven, you can free your mind

  1. Create a Jar with Dependencies using maven-shade-plugin

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>2.3</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <finalName>jar_file_name</finalName>
            <outputDirectory>dist</outputDirectory>
            <minimizeJar>true</minimizeJar>
            <filters>
              <filter>
                <artifact>mysql:mysql-connector-java</artifact>
                <includes>
                  <include>**</include>
                </includes>
              </filter>
            </filters>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    and you run it with

    mvn clean package
    
  2. Create a Jar with Dependencies using maven-assembly-plugin

    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
      <archive>
        <manifest>
        <mainClass>fully.qualified.MainClass</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      </configuration>
    </plugin>
    

    and you run it with

    mvn clean compile assembly:single
    

    Compile goal should be added before assembly:single or otherwise the code on your own project is not included.

    Commonly this goal is tied to a build phase to execute automatically. This ensures the JAR is built when executing mvn install or performing a deployment/release.

    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id> <!-- this is used for inheritance merges -->
          <phase>package</phase> <!-- bind to the packaging phase -->
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    
  3. Create a Jar with Dependencies using maven-dependency-plugin

    You can use the dependency-plugin to generate all dependencies in a separate directory before the package phase and then include that in the classpath of the manifest:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-dependencies</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <outputDirectory>${project.build.directory}/lib</outputDirectory>
            <overWriteReleases>false</overWriteReleases>
            <overWriteSnapshots>false</overWriteSnapshots>
            <overWriteIfNewer>true</overWriteIfNewer>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>theMainClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
    

    Alternatively use "${project.build.directory}/classes/lib" as OutputDirectory to integrate all jar-files into the main jar, but then you will need to add custom classloading code to load the jars.

  4. Delete additional directories and files using maven-clean-plugin

    The Maven Clean Plugin will delete the target directory by default. You may configure it to delete additional directories and files.

    <plugin>
      <artifactId>maven-clean-plugin</artifactId>
      <version>2.5</version>
      <configuration>
        <filesets>
          <fileset>
            <directory>some/relative/path</directory>
            <includes>
              <include>**/*.tmp</include>
              <include>**/*.log</include>
            </includes>
            <excludes>
              <exclude>**/important.log</exclude>
              <exclude>**/another-important.log</exclude>
            </excludes>
            <followSymlinks>false</followSymlinks>
          </fileset>
        </filesets>
      </configuration>
    </plugin>
    

    Note: The directory in the fileset is a relative path inside a project, in other words

      <directory>some/relative/path</directory>
    

    is equivalent to:

      <directory>${basedir}/some/relative/path</directory>
    
  5. Set sourceEncoding UTF-8 for project when build

    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
  6. Compiler exclude the specified packages and their subpackages

    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
      <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <excludes>
          <exclude>**/package/subpackage1/*.java</exclude>
          <exclude>**/package/subpackage2/**/*.java</exclude>
        </excludes>
        <testExcludes>
          <exclude>**/package/subpackages/**/*.java</exclude>
        </testExcludes>
      </configuration>
    </plugin>
    

Comments

Popular posts from this blog

Reduce TIME_WAIT Socket Connections