DanielThomas opened a new pull request, #36:
URL: https://github.com/apache/tomcat-jakartaee-migration/pull/36

   It'd be incredibly useful to be able to compose this tool into different 
build workflows, so I've made some minor changes to make it easier to compose 
into other tools:
   
   - When calling `Migration.execute`, return if any conversion was necessary, 
so we can use the untouched file if no conversion was necessary
   - Allow custom profiles to be provided via an interface. I found that 
including `javax.annotation` was too broad, and we can stick with `javax` there 
as Spring 6 will continue to support the javax Pre/Post construct annotations; 
we have a _lot_ of libraries that embed those
   - Allow the default excludes to be disabled so we can configure excludes 
ourselves
   
   ## Example Gradle Project
   
   In our case, we want to use this inside [Gradle Artifact 
Transforms](https://docs.gradle.org/current/userguide/artifact_transforms.html),
 because they run at dependency resolution time, so affect compilation, test 
and IDEs, and allows these to happen transparently at build time.
   
   Here's a standalone Gradle proof-of-concept. Once this lands, I'll follow up 
with a OSS Gradle plugin under our https://github.com/nebula-plugins/ project 
that'll provide Gradle conventions for configuring this transform.
   
   ```
   buildscript {
       dependencies {
           classpath 'org.apache.tomcat:jakartaee-migration:1.0.5-SNAPSHOT'
       }
       repositories {
           mavenLocal()
       }
   }
   
   /* Example project */
   
   plugins {
        id 'java'
   }
   
   repositories {
       mavenCentral()
   }
   
   dependencies {
       implementation 'ch.qos.reload4j:reload4j:1.2.22' // javax.mail
       implementation 'com.google.guava:guava:31.1-jre' // JSR-305 
javax.annotation references should not be migrated
   }
   
   tasks.register('runtimeClasspathFiles') {
       def files = configurations.runtimeClasspath.files
       println '\nRuntime classpath files:'
       files.each { println it }
   }
   
   /* Artifact transform based migration */
   
   def artifactType = Attribute.of('artifactType', String)
   def jakartaee = Attribute.of('jakartaee', Boolean)
   
   configurations.all {
      attributes.attribute(jakartaee, true)
   }
   
   dependencies {
       attributesSchema {
           attribute(jakartaee)
       }
   
       artifactTypes.named('jar') {
           attributes.attribute(jakartaee, false)
       }
   
       registerTransform(JakartaEEMigration) {
           from.attribute(jakartaee, false).attribute(artifactType, 'jar')
           to.attribute(jakartaee, true).attribute(artifactType, 'jar')
       }
   }
   
   
   import java.nio.file.Files
   import java.util.regex.Pattern
   import org.apache.tomcat.jakartaee.Migration
   import org.apache.tomcat.jakartaee.EESpecProfile
   import org.gradle.api.artifacts.transform.TransformAction
   import org.gradle.api.artifacts.transform.TransformParameters
   
   abstract class JakartaEEMigration implements 
TransformAction<TransformParameters.None> {
       @PathSensitive(PathSensitivity.NAME_ONLY)
       @InputArtifact
       abstract Provider<FileSystemLocation> getInputArtifact()
   
       @Override
       void transform(TransformOutputs outputs) {
           def inputFile = inputArtifact.get().asFile
           def tempFilePath = Files.createTempFile("jakartaee", "transform")
           Migration migration = new Migration()
           migration.setSource(inputFile)
           migration.setDestination(tempFilePath.toFile())
           def tomcatWithoutAnnotations = 
Pattern.compile("javax([/\\.](annotation[/\\.]processing" +
                   "|ejb" +
                   "|el" +
                   "|mail" +
                   "|persistence" +
                   "|security[/\\.]auth[/\\.]message" +
                   "|servlet" +
                   "|transaction(?![/\\.]xa)" +
                   "|websocket))")
           migration.setEESpecProfile(new EESpecProfile() {
               @Override
               String getSource() {
                   return 'javax'
               }
   
               @Override
               String getTarget() {
                   return 'jakarta'
               }
   
               @Override
               Pattern getPattern() {
                   return tomcatWithoutAnnotations
               }
           })
           migration.setEnableDefaultExcludes(false)
   
           if (migration.execute()) {
               def nameWithoutExtension = inputFile.name.substring(0, 
inputFile.name.length() - 4) // these are always .jar
               def outputFile = 
outputs.file("${nameWithoutExtension}-jakartaee.jar")
               Files.move(tempFilePath, outputFile.toPath())
               println "Converted $inputFile to JakartaEE $outputFile"
               outputs.file(outputFile)
           } else {
               println "No JakartaEE transformation required for $inputFile"
               Files.delete(tempFilePath)
               outputs.file(inputFile)
           }
       }
   
   }
   ```
   
   Run `./gradlew  runtimeClasspathFiles` to see the transforms in action:
   ```
   No JakartaEE transformation required for 
/Users/dannyt/.gradle/caches/modules-2/files-2.1/com.google.guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/b421526c5f297295adef1c886e5246c39d4ac629/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar
   No JakartaEE transformation required for 
/Users/dannyt/.gradle/caches/modules-2/files-2.1/com.google.guava/failureaccess/1.0.1/1dcf1de382a0bf95a3d8b0849546c88bac1292c9/failureaccess-1.0.1.jar
   No JakartaEE transformation required for 
/Users/dannyt/.gradle/caches/modules-2/files-2.1/com.google.j2objc/j2objc-annotations/1.3/ba035118bc8bac37d7eff77700720999acd9986d/j2objc-annotations-1.3.jar
   No JakartaEE transformation required for 
/Users/dannyt/.gradle/caches/modules-2/files-2.1/com.google.errorprone/error_prone_annotations/2.11.0/c5a0ace696d3f8b1c1d8cc036d8c03cc0cbe6b69/error_prone_annotations-2.11.0.jar
   No JakartaEE transformation required for 
/Users/dannyt/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/3.0.2/25ea2e8b0c338a877313bd4672d3fe056ea78f0d/jsr305-3.0.2.jar
   No JakartaEE transformation required for 
/Users/dannyt/.gradle/caches/modules-2/files-2.1/org.checkerframework/checker-qual/3.12.0/d5692f0526415fcc6de94bb5bfbd3afd9dd3b3e5/checker-qual-3.12.0.jar
   Converted 
/Users/dannyt/.gradle/caches/modules-2/files-2.1/ch.qos.reload4j/reload4j/1.2.22/f9d9e55d1072d7a697d2bf06e1847e93635a7cf9/reload4j-1.2.22.jar
 to JakartaEE 
/Users/dannyt/.gradle/caches/transforms-3/c890cd9b691a94575775292c738cbf45/transformed/reload4j-1.2.22-jakartaee.jar
   No JakartaEE transformation required for 
/Users/dannyt/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/31.1-jre/60458f877d055d0c9114d9e1a2efb737b4bc282c/guava-31.1-jre.jar
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to