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. |
on |
on |
constantName |
String |
This is the name of the String static member variable to be generated in the output class. |
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