Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

23/01/2026

[Maven] Replace backslashes in property

Using Maven to build code on both Windows and Unix systems, you might end up with some quirky situations especially related to file path handling.

It is possible to generate altered Maven properties so that for example backslashes are converted to forward slashes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>normalize-property</id>
      <phase>initialize</phase>
      <goals>
        <goal>regex-property</goal>
      </goals>
      <configuration>
        <name>normalized.property</name>
        <value>${property}</value>
        <regex>\\</regex>
        <replacement>/</replacement>
      </configuration>
    </execution>
  </executions>
</plugin>

This can be used to alter properties that are not directly controlled by the developer, such as ${project.baseDir}, and then you can use the result as a property itself: ${normalized.property}

05/09/2024

[Java] Generate POJO from XSD in Maven

Assuming you have a nice correct XSD file with proper namespace references and all, then you could convert it to a POJO (or more) using jaxb-maven-plugin

There are multiple plugins that would achieve the same result and multiple versions of this plugin even, so searching on the web can be confusing. In year 2024, this works simply with adding a plugin in the POM:

<plugin>
  <groupId>org.jvnet.jaxb</groupId>
  <artifactId>jaxb-maven-plugin</artifactId>
  <version>4.0.8</version>
  <executions>
    <execution>
      <id>NAME_FOR_THIS_RUN</id>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <schemaDirectory>src/main/resources/FOLDER/USE_CASE</schemaDirectory> <!-- here will be the XSD -->
      </configuration>
    </execution>
  </executions>
</plugin>

31/01/2020

[Maven] Copy files to folder

Similarily to the Maven dependency plugin which copies resources from an artifact to a specific location, we can use Maven resources plugin to copy resources from a folder instead.

 <build>   
   <plugins>   
    <plugin>   
     <groupId>org.apache.maven.plugins</groupId>   
     <artifactId>maven-resources-plugin</artifactId>   
     <version>3.1.0</version>   
      <executions>   
       <execution>   
        <id>YOUR_ID</id>   
        <phase>SOME_PHASE</phase>   
        <goals>   
         <goal>copy-resources</goal>   
        </goals>   
        <configuration>    
         <outputDirectory>some/folder</outputDirectory>  
         <resources>   
          <resource>  
           <directory>some/other/folder</directory>   
          </resource>   
         </resources>   
        </configuration>   
      </execution>   
     </executions>   
    </plugin>   
   </plugins>   
  </build>   


Important parameters are:

  • outputDirectory to set where to copy the resources
  • directory to set from where to copy the resources (you can have multiple resource entries if you need to copy from multiple sources)

17/10/2019

[Maven] Run HP Fortify scan during build

If you have Fortify static code analyzer (aka HP Fortify) and intend to use it with your builds, you can configure the sca-maven-plugin to perform the scan and produce a report during the build. It can also optionally upload it to your security server.

[Maven] Code coverage during build

If you want to generate code coverage reports during your build, you can use an analyzer that will scan your code and your tests and give you a summary of the tested and untested lines of code.

[Maven] checkstyle during build

If you use autoformatting (and you should) for your code, you might also want to check for formatting violations as part of a build to ensure the style is consistent after every push. We will take a look at how to configure Google style, but this procedure works for any pair of files properly written.

21/09/2019

[Maven] Copy specific files from artifact in local directory

With Maven dependency plugin, it is possible to unpack certain content from specific artifacts in a desired directory during a certain phase:

 <build>  
   <plugins>  
     <plugin>  
       <groupId>org.apache.maven.plugins</groupId>  
       <artifactId>maven-dependency-plugin</artifactId>  
       <version>3.1.1</version>  
         <executions>  
           <execution>  
             <id>YOUR_ID</id>  
             <phase>SOME_PHASE</phase>  
             <goals>  
               <goal>unpack</goal>  
             </goals>  
             <configuration>  
               <artifactItems>  
                 <artifactItem>  
                   <groupId>SOME_GROUP</groupId>  
                   <artifactId>SOME_ARTIFACT</artifactId>  
                   <version>SOME_VERSION</version>  
                   <type>SOME_TYPE</type>  
                   <overWrite>true</overWrite>  
                   <outputDirectory>some/folder</outputDirectory>  
                   <includes>some/pattern/**,and/another/pattern/**</includes>  
                 </artifactItem>  
               </artifactItems>  
             </configuration>  
         </execution>  
       </executions>  
     </plugin>  
   </plugins>  
 </build>  


Important parameters are:

  • artifactItem to ONLY consider that artifact instead of ALL imported ones
  • type to specify the artifact type, eg: jar
  • overWrite to specify whether to overwrite existing files in output folder
  • outputDirectory to specify where to unpack the content
  • includes to limit unpacking ONLY to specific files and folders. Multiple patterns can be added by separating with a comma

[Maven] Delete folders during specific phase

Using Maven clean plugin, it is possible to specify a clean behaviour to delete only selected folders during a certain phase:

 <build>  
   <plugin>  
     <artifactId>maven-clean-plugin</artifactId>  
     <version>3.1.0</version>  
     <executions>  
       <execution>  
         <id>YOUR_ID</id>  
         <phase>SOME_PHASE</phase>  
         <goals>  
           <goal>clean</goal>  
         </goals>  
         <configuration>  
           <excludeDefaultDirectories>true</excludeDefaultDirectories>  
             <filesets>  
               <fileset>  
                 <directory>some/directory</directory>  
               </fileset>  
             </filesets>  
         </configuration>  
       </execution>  
     </executions>  
   </plugin>  
 </build>  


Important parameters are:

  • excludeDefaultDirectories to avoid cleaning also the directories that a normal mvn:clean execution would
  • fileset to specify only the directories to delete

22/08/2019

[Maven] Generate Avro sources at compile time

If you are using Apache Avro in your project, you might want to automatize the sources generation step so that at every compile, the target classes are built from the Avro schema.

In Maven, this can be done with the avro-maven-plugin with a configuration as such:

 <project>  
   <modelVersion>4.0.0</modelVersion>  
   <groupId>com.blogspot.groglogs</groupId>  
   <artifactId>sample-avro-maven-pom</artifactId>  
   <version>1</version>  
   
   <dependencies>  
     <dependency>  
       <groupId>org.apache.avro</groupId>  
       <artifactId>avro</artifactId>  
     </dependency>  
   
     <!--   
     Use this for testing purposes only if you wish to manually generate the classes from Avro sources:  
   
     java -jar avro-tools.jar compile schema SCHEMA_FILE.avsc TARGET_DIRECTORY  
   
     See: https://avro.apache.org/docs/current/gettingstartedjava.html  
     <dependency>  
       <groupId>org.apache.avro</groupId>  
       <artifactId>avro-tools</artifactId>  
       <scope>provided</scope>  
     </dependency>  
     -->  
   </dependencies>  
   
   <build>  
     <plugins>  
       <plugin>  
         <groupId>org.apache.avro</groupId>  
         <artifactId>avro-maven-plugin</artifactId>  
         <executions>  
           <execution>  
             <phase>generate-sources</phase>  
             <goals>  
               <goal>schema</goal>  
             </goals>  
             <configuration>  
               <sourceDirectory>SOURCE_DIR</sourceDirectory>  
               <outputDirectory>DEST_DIR</outputDirectory>  
             </configuration>  
           </execution>  
         </executions>  
       </plugin>  
     </plugins>  
   </build>  
 </project>  


Remember that it is also possible to use avro-tools to manually generate sources from a schema.