Include the source code generated by Doma in the source JAR

Overview

Doma uses the Pluggable Annotation Processing API to automatically generate DAO implementation classes and entity auxiliary classes at compile time. Java files are automatically generated by Doma, but even if you create a source JAR by the method normally (?) Introduced in Maven or Gradle (see the link below), the automatically generated Java files are the source. Not included in the JAR.

Entities that are mapped one-to-one with a table are generated by Doma-Gen and collectively referenced in a JAR from a Web project or batch project. At that time, there is a motivation to include the Java file automatically generated by Doma in the source JAR of the entity.

Here, I will describe how to include Java files automatically generated by Doma in the source JAR for Maven and Gradle respectively.

Maven

To create a source JAR in Maven, run the jar goal of ʻorg.apache.maven.plugins: maven-source-plugin`.

Since the target of packaging is src / main / java and src / main / resources, it seems good to add the output directory of the Java file automatically generated by Doma there. With that in mind, I looked at the jar goal documentation, but I can add packaging targets. I couldn't find such an option.

There is no choice but to implement the jar goal [SourceJar Mojo](https://github.com/apache/maven-source-plugin/blob/maven-source-plugin-3.0.1/src/main/java/ I looked at org / apache / maven / plugins / source / SourceJarMojo.java). SourceJarMojo is [SourceJarNoForkMojo](https://github.com/apache/maven-source-plugin/blob/maven-source-plugin-3.0.1/src/main/java/org/apache/maven/plugins/ It inherited from source / SourceJarNoForkMojo.java). In SourceJarNoForkMojo, [where the source directory (the target of packaging) is acquired](https://github.com/apache/maven-source-plugin/blob/maven-source-plugin-3.0.1 Looking at /src/main/java/org/apache/maven/plugins/source/SourceJarNoForkMojo.java#L73), MavenProject # getCompileSourceRoots () is called. Javadoc of MavenProject # getCompileSourceRoots () It was not written, but it seems to be a setting related to the entire project from the class name etc.

From the above, I decided to add the output destination of the Java file automatically generated by Doma as a resource directory.

<resources>
    <resource>
        <filtering>false</filtering>
        <directory>src/main/resources</directory>
    </resource>
    <resource>
        <filtering>false</filtering>
        <!--
Default output destination of source code generated by Pluggable Annotation Processing API
Sources by adding to the resource directory-Include in jar.
        https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#generatedSourcesDirectory
        -->
        <directory>${project.build.directory}/generated-sources/annotations</directory>
    </resource>
</resources>

However, Maven copies the resources, compiles them, and packages them in a JAR, so if you do mvn package several times, the second and subsequent JARs (those whose class files are packaged, not the source JAR) The Java file automatically generated by Doma will also be packaged in the JAR).

Therefore, I added a setting that does not include Java files in the package in the setting of ʻorg.apache.maven.plugins: maven-jar-plugin`.

<!--
I added the source directory to the resource so it's in a JAR file
Avoid including Java files.
-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/*.java</exclude>
        </excludes>
    </configuration>
</plugin>

All you have to do now is mvn package source: jar.

Gradle

Unlike Maven, Gradle does not have a plugin to create a source JAR, but defines a task to create a source JAR by yourself based on the Jar task.

At that time, add the output destination of the Java file automatically generated by Doma to the package target directory.

In Gradle, the default destination for Java files automatically generated by Doma is sourceSets.main.output.classesDirs. Since this is also the output destination of the class file, write the settings excluding the class file.

task sourcesJar(type: Jar) {
    from sourceSets.main.allJava, sourceSets.main.output.classesDirs //The source generated by Doma is sourceSets.main.output.Output to classesDirs
    classifier = 'sources'
    exclude '**/*.class' //sourceSets.main.output.classesDirs also includes class files, so filter
}

As mentioned above, Java files that are automatically generated by Doma (or rather generated using the Pluggable Annotation Processing API) are output in the same location as the class files. Therefore, the Java file is packaged in the JAR file by default, so add a setting that excludes it (why is it output to such a location by default?).

//sourceSets.main.output.Excluding Java files output to classesDirs
jar.excludes = ['**/*.java']

With this, all you have to do is gradle build sources Jar.

At the end

The working code example is in the following location.

You can even experience deploying the source JAR to your local Nexus.

Recommended Posts

Include the source code generated by Doma in the source JAR
How to debug the generated jar file in Eclipse
When reading the source code
Guess the character code in Java
Pre-written source code for the activity
Include image resources in jar file
[Reference example] Copyright is described in the comment of the source code.
What to do if you don't like the code generated by swagger-codegen-cli
I checked the package name referenced by the class contained in the JAR file
The application absorbs the difference in character code
[HTTP] Status code included in the HTTP response
Sort by multiple fields in the class
Preventing mistakes in the Logger name by copying
Technology for reading Java source code in Eclipse
[Java] Judgment by entering characters in the terminal