Monday, November 28, 2022

Maven helper plugin - add sources, compile plugin - include/exclude sources, JAR plugin - include/exclude classes, create additional JARs from module with classifier

 HOWTO

Control what is compiled in Maven module - non-standard configuration

Input

  • Add other Java sources 
  • Add other resource files
  • Filter sources

Output 

  •  Filter classes  added into JAR
  •  Create additional JARs from compiled sources

buider-helper-maven-plugin configuration

add sources from other modules
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/generated</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

maven-compile-plugin configuration 

  • define different main source dir
  • add sources from other modules
<build>
    <sourceDirectory>../moduleB</sourceDirectory>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
        <configuration>
        <includes>
            <include>../moduleC/src/main/java/**/*.java</include>
            <include>../moduleD/src/main/java/**/*.java</include>
        </includes>
        </configuration>
        </plugin>
    </plugins>
</build>

maven-resources-plugin

  • add additional files to resources to be included in package
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>


maven-jar-plugin configuration 

  • - include/exclude classes from module JAR
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <includes>
                        <include>**/generics/*</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>test-ldap</classifier>
                            <includes>
                                <include>**/ldap/*</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

JARs created

[INFO] --- maven-jar-plugin:3.3.0:jar (default-jar) @ tutorial ---
[INFO] Building jar: /git/java-tutorial/target/tutorial.jar
[INFO] 
[INFO] --- maven-jar-plugin:3.3.0:jar (default) @ tutorial ---
[INFO] Building jar: /git/java-tutorial/target/tutorial-test-ldap.jar

No comments:

Post a Comment