Skip to content

Instantly share code, notes, and snippets.

@jkot
Last active July 2, 2021 13:16
Show Gist options
  • Save jkot/8668441 to your computer and use it in GitHub Desktop.
Save jkot/8668441 to your computer and use it in GitHub Desktop.
Maven tricks

Maven tricks

Echo all available Maven properties

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-antrun-plugin</artifactId>
	<version>1.7</version>
	<executions>
		<execution>
			<phase>validate</phase>
			<goals>
				<goal>run</goal>
			</goals>
			<configuration>
				<tasks>
					<echoproperties />
				</tasks>
			</configuration>
		</execution>
	</executions>
</plugin>

May not display all the properties: http://stackoverflow.com/questions/12317609/maven-overview-for-the-values-of-maven-properties

Defining a project root property

Maven makes it very difficult to refer to other directories than the current working directory (from which the mvn command was executed) - it is this annoying on purpose. A workaround for Maven >= 3.0.4:

<property name="main.basedir" location="./.." />
<echo message="main.basedir=${main.basedir}"/>   

Jared's Answer @ Maven2 property that indicates the parent directory

Interactively resolving Maven variables/properties

mvn help:evaluate

Might be helpful for debugging

mvn help:effective-pom - Displays the effective POM as an XML for this build, with the active profiles factored in. mvn help:expressions - Displays the supported Plugin expressions used by Maven. mvn help:help -Ddetail=true - Display help information on maven-help-plugin. help:system - Displays a list of the platform details like system properties and environment variables.

Hadoop job jar assembly

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>job</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>  
  <!-- include packed *runtime* dependency jars in lib/ -->
    <dependencySet> 
      <unpack>false</unpack>
      <scope>runtime</scope>
      <outputDirectory>lib</outputDirectory>
      <excludes>
        <exclude>...</exclude> <!-- -->
      </excludes>
    </dependencySet>
  <!-- include unpacked classes that are needed to locally run any main class from the job jar -->
    <dependencySet> 
      <unpack>true</unpack>
      <scope>runtime</scope>
      <outputDirectory>/</outputDirectory>
	  <useTransitiveFiltering>true</useTransitiveFiltering> <!-- e.g. exclude the logging libraries wherever they are transitively -->
      <includes>
        <include>...</include> <!-- include one dependency -->
      </includes>
      <excludes>
      	<exclude>ch.qos.logback:logback-classic</exclude> <!-- etc. -->
      </excludes>
    </dependencySet>
 <!-- include another dependency, same as above -->
    <dependencySet> </dependencySet>
  </dependencySets> 
  
 <!-- include build output (target/classes) but exclude jars and the output directory -->
   <fileSets>
       <fileSet>
           <directory>${basedir}/target/classes</directory>
           <outputDirectory>/</outputDirectory>
           <excludes>
               <exclude>*.jar</exclude>
               <exclude>output/</exclude>
           </excludes>
       </fileSet>
   </fileSets>
</assembly>

Maven versions

mvn versions:display-dependency-updates - shows which dependencies can be upgraded to newer versions.

Code checks

http://errorprone.info/

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <compilerId>javac-with-errorprone</compilerId>
          <forceJavacCompilerUse>true</forceJavacCompilerUse>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-javac-errorprone</artifactId>
            <version>2.5</version>
          </dependency>
        </dependencies>
      </plugin>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment