You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by pk...@apache.org on 2021/05/07 13:05:55 UTC

[uima-ruta] 01/01: [UIMA-6241] Ruta maven plugin does not fail the build when it fails

This is an automated email from the ASF dual-hosted git repository.

pkluegl pushed a commit to branch bugfix/UIMA-6241-Ruta-maven-plugin-does-not-fail-the-build-when-it-fails
in repository https://gitbox.apache.org/repos/asf/uima-ruta.git

commit 79989c78180b43f6eb985df36eea5a2f7e3f21a1
Author: Peter Klügl <pe...@averbis.com>
AuthorDate: Fri May 7 15:05:15 2021 +0200

    [UIMA-6241] Ruta maven plugin does not fail the build when it fails
    
    - adding failOnError with default true
---
 .../ruta/maven/RutaGenerateDescriptorMojo.java     | 40 ++++++++++++++-----
 .../uima/ruta/maven/RutaGenerateMTWLMojo.java      | 45 +++++++++++++++-------
 .../uima/ruta/maven/RutaGenerateTWLMojo.java       | 42 +++++++++++++-------
 3 files changed, 91 insertions(+), 36 deletions(-)

diff --git a/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateDescriptorMojo.java b/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateDescriptorMojo.java
index 8c39071..7165b50 100644
--- a/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateDescriptorMojo.java
+++ b/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateDescriptorMojo.java
@@ -198,6 +198,12 @@ public class RutaGenerateDescriptorMojo extends AbstractMojo {
   @Parameter(required = false)
   private String[] buildPaths;
 
+  /**
+   * Fail on error.
+   */
+  @Parameter(defaultValue = "true", required = true)
+  private boolean failOnError;
+
   @Override
   public void execute() throws MojoExecutionException, MojoFailureException {
 
@@ -267,14 +273,14 @@ public class RutaGenerateDescriptorMojo extends AbstractMojo {
       try {
         factory.setDefaultTypeSystem(typeSystemTemplate.toURI().toURL());
       } catch (MalformedURLException e) {
-        getLog().warn("Failed to get URL of " + typeSystemTemplate, e);
+        handleError("Failed to get URL of " + analysisEngineTemplate, e);
       }
     }
     if (analysisEngineTemplate != null) {
       try {
         factory.setDefaultEngine(analysisEngineTemplate.toURI().toURL());
       } catch (MalformedURLException e) {
-        getLog().warn("Failed to get URL of " + analysisEngineTemplate, e);
+        handleError("Failed to get URL of " + analysisEngineTemplate, e);
       }
     }
 
@@ -303,9 +309,9 @@ public class RutaGenerateDescriptorMojo extends AbstractMojo {
                 options);
         toBuild.add(descriptorInformation);
       } catch (RecognitionException re) {
-        getLog().warn("Failed to parse UIMA Ruta script file: " + file.getAbsolutePath(), re);
+        handleError("Failed to parse UIMA Ruta script file: " + file.getAbsolutePath(), re);
       } catch (IOException ioe) {
-        getLog().warn("Failed to load UIMA Ruta script file: " + file.getAbsolutePath(), ioe);
+        handleError("Failed to load UIMA Ruta script file: " + file.getAbsolutePath(), ioe);
       }
     }
 
@@ -335,7 +341,7 @@ public class RutaGenerateDescriptorMojo extends AbstractMojo {
 
     for (RutaDescriptorInformation eachFailed : toBuild) {
       String scriptName = eachFailed.getScriptName();
-      getLog().warn("Failed to build UIMA Ruta script: " + scriptName);
+      handleError("Failed to build UIMA Ruta script: " + scriptName);
     }
 
   }
@@ -498,7 +504,7 @@ public class RutaGenerateDescriptorMojo extends AbstractMojo {
             RutaGenerateDescriptorMojo.class.getClassLoader());
   }
 
-  private void addRutaNature() {
+  private void addRutaNature() throws MojoExecutionException {
 
     File projectDir = project.getFile().getParentFile();
     File projectFile = new File(projectDir, ".project");
@@ -534,14 +540,14 @@ public class RutaGenerateDescriptorMojo extends AbstractMojo {
         try {
           FileUtils.fileWrite(projectFile, encoding, string);
         } catch (IOException e) {
-          getLog().warn("Failed to write .project file", e);
+          handleError("Failed to write .project file", e);
         }
       }
       buildContext.refresh(projectFile);
     }
   }
 
-  private void addRutaBuildPath() {
+  private void addRutaBuildPath() throws MojoExecutionException {
     File projectDir = project.getFile().getParentFile();
 
     if (buildPaths == null || buildPaths.length == 0) {
@@ -575,7 +581,7 @@ public class RutaGenerateDescriptorMojo extends AbstractMojo {
       try {
         FileUtils.fileWrite(buildpathFile, encoding, string);
       } catch (IOException e) {
-        getLog().warn("Failed to write .buildpath file", e);
+        handleError("Failed to write .buildpath file", e);
       }
     }
     buildContext.refresh(buildpathFile);
@@ -611,4 +617,20 @@ public class RutaGenerateDescriptorMojo extends AbstractMojo {
     attributes.addChild(attribute);
     buildpathNode.addChild(buildpathentry);
   }
+
+  private void handleError(String message, Exception e) throws MojoExecutionException {
+    if (failOnError) {
+      throw new MojoExecutionException(message, e);
+    }
+
+    getLog().error(message, e);
+  }
+
+  private void handleError(String message) throws MojoExecutionException {
+    if (failOnError) {
+      throw new MojoExecutionException(message);
+    }
+
+    getLog().error(message);
+  }
 }
diff --git a/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateMTWLMojo.java b/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateMTWLMojo.java
index c9fb1f3..254824c 100644
--- a/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateMTWLMojo.java
+++ b/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateMTWLMojo.java
@@ -44,8 +44,8 @@ import org.sonatype.plexus.build.incremental.BuildContext;
  */
 @Mojo(name = "mtwl", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresDependencyResolution = ResolutionScope.COMPILE)
 public class RutaGenerateMTWLMojo extends AbstractMojo {
-  
-  @Parameter( defaultValue = "${project}", readonly = true )
+
+  @Parameter(defaultValue = "${project}", readonly = true)
   private MavenProject project;
 
   @Component
@@ -75,34 +75,41 @@ public class RutaGenerateMTWLMojo extends AbstractMojo {
   @Parameter(defaultValue = "true", required = true)
   private boolean compress;
 
+  /**
+   * Fail on error.
+   */
+  @Parameter(defaultValue = "true", required = true)
+  private boolean failOnError;
+
+  @Override
   public void execute() throws MojoExecutionException, MojoFailureException {
     File parentFile = outputFile.getParentFile();
     if (!parentFile.exists()) {
       parentFile.mkdirs();
       buildContext.refresh(parentFile);
     }
-    
+
     this.project.addCompileSourceRoot(parentFile.getPath());
 
     List<File> files = null;
     try {
       files = getFilesIfModifiedOrNotExists(inputFiles, outputFile, buildContext);
     } catch (IOException e) {
-      getLog().warn("Error accessing input files.", e);
+      handleError("Error accessing input files.", e);
     }
 
-    if(files == null || files.isEmpty()) {
+    if (files == null || files.isEmpty()) {
       getLog().info("No modified files to process... skipping.");
       return;
     }
-    
+
     getLog().info("Processing following files: " + files.toString());
-    
+
     MultiTreeWordList trie = null;
     try {
       trie = new MultiTreeWordList(files, new File(inputFiles.getDirectory()));
     } catch (IOException e) {
-      getLog().warn("Error creating MTWL file.", e);
+      handleError("Error creating MTWL file.", e);
     }
 
     if (trie != null) {
@@ -110,18 +117,19 @@ public class RutaGenerateMTWLMojo extends AbstractMojo {
         trie.createMTWLFile(outputFile.getAbsolutePath(), compress, encoding);
         buildContext.refresh(outputFile);
       } catch (IOException e) {
-        getLog().warn("Error writing MTWL file.", e);
+        handleError("Error writing MTWL file.", e);
       }
     }
 
   }
-  
-  public static List<File> getFilesIfModifiedOrNotExists(FileSet fileSet, File outputFile, BuildContext buildContext) throws IOException {
+
+  public static List<File> getFilesIfModifiedOrNotExists(FileSet fileSet, File outputFile,
+          BuildContext buildContext) throws IOException {
     List<File> result = new ArrayList<File>();
 
     boolean exists = outputFile.exists();
     long outputModified = outputFile.lastModified();
-    
+
     File directory = new File(fileSet.getDirectory());
     String includes = Utils.toString(fileSet.getIncludes());
     String excludes = Utils.toString(fileSet.getExcludes());
@@ -131,16 +139,25 @@ public class RutaGenerateMTWLMojo extends AbstractMojo {
       if (each instanceof File) {
         File file = (File) each;
         long inputModified = file.lastModified();
-        if(inputModified > outputModified) {
+        if (inputModified > outputModified) {
           modified = true;
         }
         result.add(file);
       }
     }
-    if(!exists || modified) {
+    if (!exists || modified) {
       return result;
     } else {
       return Collections.emptyList();
     }
   }
+
+  private void handleError(String message, Exception e) throws MojoExecutionException {
+    if (failOnError) {
+      throw new MojoExecutionException(message, e);
+    }
+
+    getLog().error(message, e);
+  }
+
 }
diff --git a/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateTWLMojo.java b/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateTWLMojo.java
index 15fe677..961014e 100644
--- a/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateTWLMojo.java
+++ b/ruta-maven-plugin/src/main/java/org/apache/uima/ruta/maven/RutaGenerateTWLMojo.java
@@ -46,8 +46,8 @@ import org.sonatype.plexus.build.incremental.BuildContext;
  */
 @Mojo(name = "twl", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresDependencyResolution = ResolutionScope.COMPILE)
 public class RutaGenerateTWLMojo extends AbstractMojo {
-  
-  @Parameter( defaultValue = "${project}", readonly = true )
+
+  @Parameter(defaultValue = "${project}", readonly = true)
   private MavenProject project;
 
   @Component
@@ -77,6 +77,13 @@ public class RutaGenerateTWLMojo extends AbstractMojo {
   @Parameter(defaultValue = "true", required = true)
   private boolean compress;
 
+  /**
+   * Fail on error.
+   */
+  @Parameter(defaultValue = "true", required = true)
+  private boolean failOnError;
+
+  @Override
   public void execute() throws MojoExecutionException, MojoFailureException {
     if (!outputDirectory.exists()) {
       outputDirectory.mkdirs();
@@ -84,14 +91,14 @@ public class RutaGenerateTWLMojo extends AbstractMojo {
     }
 
     this.project.addCompileSourceRoot(outputDirectory.getPath());
-    
+
     Map<File, File> inputOutputMap = null;
     try {
-      inputOutputMap =  getModifiedFilesMap(inputFiles, outputDirectory, buildContext);
+      inputOutputMap = getModifiedFilesMap(inputFiles, outputDirectory, buildContext);
     } catch (IOException e) {
-      getLog().warn("Error accessing input files.", e);
+      handleError("Error accessing input files.", e);
     }
-    
+
     if (inputOutputMap == null || inputOutputMap.isEmpty()) {
       getLog().debug("No modified files to process... skipping.");
       return;
@@ -106,7 +113,7 @@ public class RutaGenerateTWLMojo extends AbstractMojo {
       try {
         list = new TreeWordList(inputFile.getAbsolutePath(), false);
       } catch (IOException e) {
-        getLog().warn("Error generating twl.", e);
+        handleError("Error generating twl.", e);
       }
       if (list != null) {
         try {
@@ -114,18 +121,19 @@ public class RutaGenerateTWLMojo extends AbstractMojo {
           list.createTWLFile(outputFile.getAbsolutePath(), compress, encoding);
           buildContext.refresh(outputFile);
         } catch (IOException e) {
-          getLog().warn("Error writing twl file.", e);
+          handleError("Error writing twl file.", e);
         }
       }
     }
 
   }
 
-  private Map<File, File> getModifiedFilesMap(FileSet fileSet, File outputDirectory, BuildContext buildContext) throws IOException {
+  private Map<File, File> getModifiedFilesMap(FileSet fileSet, File outputDirectory,
+          BuildContext buildContext) throws IOException {
     Map<File, File> result = new LinkedHashMap<>();
 
     File directory = new File(fileSet.getDirectory());
-    
+
     String includes = Utils.toString(fileSet.getIncludes());
     String excludes = Utils.toString(fileSet.getExcludes());
 
@@ -133,7 +141,8 @@ public class RutaGenerateTWLMojo extends AbstractMojo {
       if (each instanceof File) {
         File inputFile = (File) each;
         File outputFile = getOutputFile(inputFile, directory, outputDirectory);
-        if (outputFile == null || !outputFile.exists() || inputFile.lastModified() > outputFile.lastModified()) {
+        if (outputFile == null || !outputFile.exists()
+                || inputFile.lastModified() > outputFile.lastModified()) {
           result.put(inputFile, outputFile);
         }
       }
@@ -144,11 +153,11 @@ public class RutaGenerateTWLMojo extends AbstractMojo {
   private File getOutputFile(File inputFile, File inputDirectory, File outputDirectory) {
     String inputName = inputFile.getName();
     String outputName = inputName.substring(0, inputName.length() - 3) + "twl";
-    
+
     Path inputFilePath = inputFile.toPath();
     Path inputDirectoryPath = inputDirectory.toPath();
     Path outputDirectoryPath = outputDirectory.toPath();
-    
+
     Path relativize = inputDirectoryPath.relativize(inputFilePath);
     Path resolve = outputDirectoryPath.resolve(relativize);
     Path parent = resolve.getParent();
@@ -156,4 +165,11 @@ public class RutaGenerateTWLMojo extends AbstractMojo {
     return result.toFile();
   }
 
+  private void handleError(String message, Exception e) throws MojoExecutionException {
+    if (failOnError) {
+      throw new MojoExecutionException(message, e);
+    }
+
+    getLog().error(message, e);
+  }
 }