Skip to main content

Useful maven recipes

 Filtering java sources using maven properties

As you probably know, the maven resource plugin gives you the ability to create resources directory where the resource files will be filtered so that any maven properties defined in the build can be interpolated or substituted.  This allows you for example to create a properties file like:

artifactid=${project.artifactId}

when packaging this properties file the property artifactid will be set to the value of the maven project's artifactId.

When using annotations, the strings used in the parameters must be know at compile time and if you want to use a maven property such as project.version, you cannot use the previous property file since the string will only be created at runtime.  This is where java filtered source files comes into play.

What you need to do is in the parent POM shared by your application, add the following plugin executions:
      <plugin> 
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>generate-filtered-sources</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/generated-sources/filtered</outputDirectory>
              <resources>
                <resource>
                  <directory>src/main/java-filtered</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>  


Then in any maven java modules where you want to defined constants that will be known at compile time, you simply create this folder:

src/main/java-filtered

Inside this folder you create the folder structure com/greatcompany/greatapp and create the following Constants.java file:

package com.greatcompany.greatapp; 

public interface Constants {

      public static final String ARTIFACT_ID="${project.artifactId}";
      public static final String GROUP_ID="${project.groupId}";
      public static final String VERSION="${project.version}";
      public static final String PACKAGING="${project.packaging}";
      public static final String FINAL_NAME="${project.build.finalName}";

}

In any java class in your application where you need the compile time constants just say that  the class implements com.greatcompany.greatapp.Constants .

The last point I would like to make is that if you do not have the folder java-filtered then no errors will occur and that fact will be silently ignored.  This means that all you need to do to use the filtering capabilities is to create the filtered folder and put some source files.

Voila!

Comments

Popular posts from this blog

Running an I/O benchmark using IOMETER

The following document describes the methodology used at MFJ Associates for running a disk I/O benchmark.  This document assumes that the IOMETER software has been downloaded from www.iometer.org . IOMETER runs on Microsoft Windows as well as various flavors of Unix and Linux(referred to as *nix in this blog). It is made up of two components: iometer.exe a GUI program that only runs on Windows (which means you have to have at least 1 Windows desktop or server to run the GUI part) dynamo.exe or dynamo (on *nix) called the manager. In order to run a benchmark the Windows computer running iometer.exe must have TCP connectivity with the computer(s) where the benchmark will be performed.  Both must be able to connect to one another. How to run a benchmark Here is a high level view of running a benchmark. A detailed explanation will follow. You need to start the iometer.exe program on the Windows computer.  This will start the dynamo.exe program on that same compu...

Handling multipart form data in Spring 3.1

Introduction Multipart mime encoded is a format used to transmit binary and arbitrary data in 1 single HTTP request transaction. In this post, I will describe how to create and process multipart form data using Spring 3.1, the leading industry standard java application framework for creating Java web application.  I will start the discussion from the user perspective by talking about two main use cases and will expand it by describing how these two use cases translate into 7 possible application system use cases. Use Cases Here are some use cases of this feature: A browser submits or uploads a file to a web server using an HTML page. This is by far the most common use case of the multipart form data feature. A multipart is required because the form data and the file are both included in the request body. A java program (a java application or servlet instance) sends multipart form data to a web server (most likely a web service).  This...
Setting Up a WireGuard VPN: Client and Server Configuration In today's digitally connected world, secure and private communication is paramount. WireGuard is a cutting-edge VPN protocol known for its simplicity and high performance. If you're looking to set up a WireGuard VPN, here's a quick guide on what you need to do for both the client and server sides. Client Configuration The client configuration is essential for initiating a connection to the WireGuard server. Here’s what you need: WireGuard Client Software : Install the WireGuard client software on your device. Private Key : Generate a private key for the client. Server’s Public Key : Obtain the public key of the server you want to connect to. Configuration File : Create a configuration file (e.g., client.conf ) with the following settings: Client PrivateKey = <client_private_key> Address = 10.0 . 0.2 / 24 DNS = 8.8 . 8.8 [Peer] PublicKey = <server_public_key> AllowedIPs = 10.0 . 0.1 / 24 Endp...