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

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 is

Tutorial on how to write and run a javaFX 11 Spring Boot program using Eclipse

Since the decoupling of javaFX from the JDK (see http://openjfx.io ) it has become somewhat of a challenge to use javaFX with the new module capability since java 9.  This tutorial is a brief roadmap between a java 8 javafx to the java 11 version.  All code for this sample resides in https://github.com/marioja/javafx  and can be imported into eclipse and should run as is.  You can also do this manually following these instructions. First you need to use an eclipse that supports the java 11 execution environment (Eclipse 2018-12 at the time of this writing).  Then you need to make sure you have a java 11 jdk installed(11.0.2 at the time of this writing).  I downloaded openjdk11  and unzipped into a folder on my home directory (user profile on windows).  Lets call this directory jdk-11.0.2.  You should add the eclipse -vm argument in the eclipse.ini file  to select jdk-11.0.2. Start eclipse and create a brand new workspace (one that does not exist).  I called it javafx-tutorial.  If

Put your WARs on a diet with Maven: Maven and skinny wars!

I have seen a lot of confusion and misunderstanding on what is and why use a java enterprise concept called skinny war . In simple terms , a skinny war is a WAR where some of its dependencies are moved from the WAR module WEB-INF/lib to the EAR lib folder .   This post is specifically written to deal with skinny wars and does not explain or go into details which jars must be in the WAR class loader and which one do not have to . It is assumed that the reader is familiar with this concept and the concept of the WAR class loader versus the application class loader . Suffice it to say that typically, JAR that must be scanned by CDI usually belongs in the WAR class loader . Web fragments and jars with tag libraries are other candidates . Also, all test and provided scope artifacts must be specified in the WAR module as having maven remove them from the WAR does not make sense as they were never to be put there and having them in a deps POM file does not work . They will not be e