injecttext-maven

Maven Plugin to generates a Java classfile containing UTF-8 text sourced from a file on the classpath.

License

License

Categories

Categories

Maven Build Tools
GroupId

GroupId

org.ainslec
ArtifactId

ArtifactId

injecttext-maven
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

injecttext-maven
Maven Plugin to generates a Java classfile containing UTF-8 text sourced from a file on the classpath.
Project URL

Project URL

https://github.com/ainslec/injecttext
Source Code Management

Source Code Management

https://github.com/ainslec/injecttext/tree/master

Download injecttext-maven

How to add to project

<plugin>
    <groupId>org.ainslec</groupId>
    <artifactId>injecttext-maven</artifactId>
    <version>1.0.0</version>
</plugin>

Dependencies

compile (3)

Group / Artifact Type Version
org.apache.maven : maven-plugin-api jar 2.0
org.apache.maven : maven-project jar 2.0.4
org.ainslec : injecttext jar 1.0.0

provided (1)

Group / Artifact Type Version
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.2

Project Modules

There are no modules declared in this project.

Inject Text

1. Intro

Inject Text is a tiny Maven plugin for use in injecting arbitary text inside a Java member variable.

It contains a core library, which is text-in (the text in being any arbitary text) and text-out (the text out being Java Source Code in UTF-8 format).

It also contains a Maven plugin which will read (UTF-8) text from a file, and will embed it in a class in the 'generate-sources' phase of the Maven Build lifecycle.

2. What Exactly Does It Do?

The plugin will create a class containing a member variable which will be initialized to the content of a referenced file.

If the size of the text is larger than the 64K (JVM) limit to statically defined text then this library will split the text into several different text blocks, and then append all the text blocks together to make a larger string (Java itself supports strings up to 2GB but can only be embedded in Java code 64K at a time).

This approach is not optimal, but it can be useful in a certain goldilocks zone where the text size is greater than 64K but less than say 10 megabytes. Above 10 megabytes, this approach (and this plugin) is not recommended at all.

3. Alternative Approaches

Java developers would typically either:

  • Reference (or stream) a file from the filesystem.

  • Reference (or stream) a file from a network URL.

  • Access a resource from the classpath using the classloader.

  • Access a resource as a blob from a data store or a database.

The approach of statically embedding the text content into the application itself is useful if network connectivity is not guaranteed, and if the classloader is inaccessible (such as via transpiled Java). It is anticipated that use of this plugin will be very niche. Perhaps a niche of one.

4. Maven Plugin

The Maven plugin requires the following properties.

4.1. Configuration

Property Name Type Description Sample Value Default Value

inputFile

File Path

The file path to the UTF-8 text to be embedded.

${basedir}/somefile.txt

No Default

srcGenFolderBase

Folder Path

The file path to a folder that represents the base folder of the Java Source Code (to be generated)

${basedir}/src-gen/main/java

${basedir}/src-gen/main/java

packageName

String

The package name where the generated class file will be generated

your.package.name.here

No Default

className

String

The class name of the class to be generated (that contains the static member variable containing the UTF-8 text content of corresponding to the file represented by 'inputFile'.

YourClassNameHere

No Default

safeMode

String

By default, the plugin will not execute unless it sees 'src-gen' in the 'srcGenFolderBase' property. The purpose of this is to avoid accidentally overwriting an existing manually written class.

Generated source should be in a folder called 'src-gen' by convention, and the plugin can reasonably assume it’s safe to overwrite subfolders of src-gen.

To disable this safety feature provide a value of 'off' here.

on

on

constantName

String

This is the name of the String static member variable to be generated in the output class.

The code generator will actually create a number of string variables and append them in a static initialization block if the text size is too large. Only the end result will have a public accessor.

MyLibraryAsText

T

By default 'packageName' configuration property will contain a value of '${basedir}/src-gen/main/java'.

(see GWT section for additional GWT dependency)

<build>
   <plugins>
      <plugin>
         <groupId>org.ainslec</groupId>
         <artifactId>injecttext-maven</artifactId>
         <version>1.0.0</version>
         <configuration>
            <inputFile>${basedir}/somefile.txt</inputFile>
            <packageName>your.package.name.here</packageName>
            <className>YourClassNameHere</className>
         </configuration>
         <executions>
            <execution>
               <id>generate</id>
               <goals>
                  <goal>generate</goal>
               </goals>
            </execution>
         </executions>
      </plugin>
   </plugins>
</build>

5. Contact

Email : [email protected]
Twitter : @ainslec

Versions

Version
1.0.0