Annotation driven Java™ open source library for automating embedded resource file handling - a convenient alternative to Class.getResourceAsStream.
AutoRes.uk is a compile-time only dependency and does not need to be distributed with application code.
Browse the central repository
for a build system import snippet for the uk.autores:annotations
library.
Annotate a type or package and specify the resource files.
@Texts
creates one class per text file that provides the file content as a String
.
import uk.autores.*;
@Texts({"Poule.txt", "Roses.txt"})
public class PrintRhymes {
public static void main(String...args) {
System.out.println(Poule.text()
);
System.out.println(Roses.text()
);
}
}
@Messages
generates typed methods from properties files that localize and format strings.
import uk.autores.*;
import java.time.ZonedDateTime;
import java.util.Locale;
@Messages("msgs.properties")
public class PrintMessage {
public static void main(String[] args) {
var locale = Locale.getDefault();
int planet = 4;
var time = ZonedDateTime.now();
var event = "a meteor strike";
/* planet-event=At {1,time} on {1,date}, there was {2} on planet {0,number,integer}. */
String msg = Msgs.planetEvent(locale, planet, time, event)
;
System.out.println(msg);
}
}
Other annotations are
@ByteArrays
,
@Keys
,
@InputStreams
,
and the extensible @ResourceFiles
.
Versions are split into three integers: Java version; major version; minor version.
Functionally identical Java 8 and Java 11 versions are published. The Java 11 version has marginally better encapsulation when modules are in use.
Use requires static uk.autores
in
module-info.java to depend on this library at compile time only.
module example.module.name {
requires static uk.autores;
}
Apache Maven projects should use the provided scope.
<!-- Maven 3 -->
<dependency>
<groupId>uk.autores</groupId>
<artifactId>annotations</artifactId>
<version>${autores.version}</version>
<scope>provided</scope>
</dependency>
If necessary make the compiler aware of the annotation processor using annotationProcessorPaths.
<!-- Maven 3 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>uk.autores</groupId>
<artifactId>annotations</artifactId>
<version>${autores.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
Gradle projects must add resource files to the classpath. The library must be exposed to the annotation processor.
// Gradle 8; Kotlin syntax
var autores = "uk.autores:annotations:" + autoresVersion
dependencies {
compileOnly(files("src/main/resources"))
compileOnly(autores)
annotationProcessor(autores)
}