Including and excluding source files and folders

As per Maven conventions, all project sources should be in the src folder. However, there may be legacy projects that are organized differently and may have more than one source folder. Also, in some projects, we might generate sources dynamically from tools such as wsdl2java. In such cases, Maven needs to be told about these additional source folders. Note that such projects may not work well in IDEs.

How to do it...

Use the following steps to include and exclude source files and folders in your Maven project:

  1. Open the Maven project named project-with-additional-source.
  2. Add the following section in the pom file:
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.9.1</version>
            <executions>
              <execution>
                <id>add-source</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>add-source</goal>
                </goals>
                <configuration>
                  <sources>
                    <source>src/main/source</source>
                  </sources>
                </configuration>
              </execution>
            </executions>
        </plugin>
  3. Run the following command:
    mvn compile
    
  4. See the output generated:
    [INFO] --- build-helper-maven-plugin:1.9.1:add-source (add-source) @ project-with-additional-source ---
    [INFO] Source directory: C:projectsapache-maven-cookbookproject-with-additional-sourcesrcmainsource added.
    
  5. View the target/classes folder:
    How to do it...

How it works...

We had an additional source folder called src/main/source. We specified this in the configuration section of the Build Helper Maven plugin. We also bound the add-source goal of the plugin to the generate-sources phase of the default lifecycle.

As part of the default lifecycle, the generate-sources phase is run by Maven prior to the compile goal. This invokes the add-source goal of the plugin, which adds the additional folder and its contents for compilation.

In a similar way, additional test folders can be added to the build. The configuration would be identical to the earlier case, except for the execution section, which would be as follows:

         <execution>
            <id>add-test-source</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/main/source</source>
              </sources>
            </configuration>
          </execution> 

We specify the add-test-source goal instead of add-source and bind it to the generate-test-sources phase.

There's more...

The Build Helper Maven plugin provides a number of other goals that meet specific project requirements. Here are some of them:

  • attach-artifact: This is used to attach additional artifacts to be installed and/or deployed, besides the project artifact. This would be done by configuring the plugin as follows:
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.9.1</version>
            <executions>
              <execution>
                <id>attach-artifacts</id>
                <phase>package</phase>
                <goals>
                  <goal>attach-artifact</goal>
                </goals>
                <configuration>
                  <artifacts>
                    <artifact>
                      <file>some file</file>
                      <type>extension of your file </type>
                      <classifier>optional</classifier>
                    </artifact>
                  </artifacts>
                </configuration>
              </execution>
            </executions>
          </plugin>
  • maven-version: This is used to set a property containing the current version of Maven, which can be used as required. To use the Maven version number in the manifest of the project JAR, we will configure the plugin as follows:
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.9.1</version>
            <executions>
              <execution>
                <id>maven-version</id>
                <goals>
                  <goal>maven-version</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
            <configuration>
              <archive>
                <manifestEntries>
                  <Maven-Version>${maven.version}</Maven-Version>
                </manifestEntries>
              </archive>
            </configuration>
          </plugin>
        </plugins>
      </build>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.119.170