November 22, 2010

Maven Tip: Finding Default Project Settings

Originally published 25 Nov 2009

Have you ever had to override a default project setting in Maven and didn't know the exact setting?  A google search could do the trick, but here I'll describe another way.

As an example, suppose you're converting an existing Ant build that doesn't follow the standard Maven project structure.  Maybe your project puts its source and test code right under src and test.

You start the conversion by creating a bare bones POM:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany</groupId>
  <artifactId>someproject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Then, you think: "Okay.  My project structure doesn't follow the defaults.  I'll want to change this eventually, but I want to see if this POM is okay.  I don't like to go long periods of time without seeing something working.  How do I tell Maven to change where it should look for source and test code?"

By running mvn help:effective-pom, you can find this quickly:

<project xmlns="http://maven.apache.org/POM/4.0.0"
  ...
  <build>
    <sourceDirectory>.../someproject/src/main/java</sourceDirectory>
    <testSourceDirectory>.../someproject/src/test/java</testSourceDirectory>
  ...

"Oh yeah.  There are the settings.  All I need to do is change this in my POM"

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany</groupId>
  <artifactId>someproject</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <testSourceDirectory>test</testSourceDirectory>
  </build>  
</project>

The help:effective-pom goal is a trick I use to quickly look up a project setting as well as to see all the settings together for a given project or module.

No comments:

Post a Comment