Using project dependencies’ sources


References

The problem

<project>

  <groupId>her.group</groupId>
  <artifactId>her-app</artifactId>
  <version>her.version</version>

  <dependencies>
    <dependency>
      <groupId>his.group</groupId>
      <artifactId>his-lib</artifactId> <!-- compiled targeting 1.7 -->
      <version>his.version</version>
      <scope>compile</scope>
    <dependency>
  </dependencies>

  <build>
    <plugins>

      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>

    </plugins>
  </build>

</project>

Step 1: Changing the dependencies’ scope

<project>

  <dependencies>

    <dependency>
      <groupId>his.group</groupId>
      <artifactId>his-lib</artifactId> <!-- compiled targeting 1.7 -->
      <version>his.version</version>
      <scope>runtime</scope> <!-- we don't even require for compilation -->
    <dependency>

  </dependencies>

</project>

Step 2: Unpacking dependencies’ sources

<project>

  <build>
    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>unpack-dependencies-sources</id>
            <phase>generate-sources</phase> <!-- default: process-sources -->
            <goals>
              <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
              <classifier>sources</classifier>
              <failOnMissingClassifierArtifact>
                true
              </failOnMissingClassifierArtifact>
              <includeArtifactIds>his-lib</includeArtifactIds>
              <outputDirectory>
                ${project.build.directory}/unpacked-dependencies-sources
              </outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>

</project>

Step 3: Adding unpacked dependencies’ sources

<project>

  <build>
    <plugins>

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>add-unpacked-dependencies-sources</id>
            <phase>generate-sources</phase> <!-- default binding phase -->
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>
                  ${project.build.directory}/unpacked-dependencies-sources
                </source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>

</project>

Notes

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s