Merge branch 'main' into bugfix/UIMA-6241-Ruta-maven-plugin-does-not-fail-the-build-when-it-fails
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 @@
   @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 @@
       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 @@
                 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 @@
 
     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 @@
             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 @@
         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 @@
       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 @@
     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 @@
  */

 @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 @@
   @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 @@
         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 @@
       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 @@
  */

 @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 @@
   @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 @@
     }

 

     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 @@
       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 @@
           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 @@
       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 @@
   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 @@
     return result.toFile();

   }

 

+  private void handleError(String message, Exception e) throws MojoExecutionException {

+    if (failOnError) {

+      throw new MojoExecutionException(message, e);

+    }

+

+    getLog().error(message, e);

+  }

 }