Skip to content

Commit

Permalink
Fix ignoreJspFragmentErrors parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
tcollignon committed Feb 26, 2020
1 parent ae01a39 commit abd290f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 26 deletions.
83 changes: 57 additions & 26 deletions src/main/java/io/leonard/maven/plugins/jspc/JspcMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,44 @@
//========================================================================
package io.leonard.maven.plugins.jspc;

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.concurrent.*;

import javax.xml.XMLConstants;
import javax.xml.parsers.*;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;

import org.apache.jasper.*;
import org.apache.jasper.JasperException;
import org.apache.jasper.JspC;
import org.apache.jasper.TrimSpacesOption;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.*;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.*;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.concurrent.*;

/**
* <p>
* This goal will compile jsps for a webapp so that they can be included in a
Expand Down Expand Up @@ -166,7 +181,7 @@ public class JspcMojo extends AbstractMojo {
*/
@Parameter(defaultValue = "true")
private boolean validateWebXmlAfterMerge;

/**
* If true, validates web.xml file (xsd see webXmlXsdSchema parameter)
* after beeing merge if mergeFragment parameter is true
Expand All @@ -180,19 +195,19 @@ public class JspcMojo extends AbstractMojo {
*/
@Parameter(defaultValue = "http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd")
private String webXmlXsdSchema;

/**
* Optionnal hostname of http proxy (use when validating dtd/xsd with external URL)
*/
@Parameter
private String httpProxyHost;

/**
* Optionnal port of http proxy (use when validating dtd/xsd with external URL)
*/
@Parameter
private String httpProxyPort;

/**
* Optionnal no proxy hosts (use when validating dtd/xsd with external URL)<br>
* A list of hosts that should be reached directly, bypassing the proxy. <br>
Expand All @@ -203,7 +218,7 @@ public class JspcMojo extends AbstractMojo {
*/
@Parameter
private String httpNoProxyHosts;

private boolean proxyEnvSet;
private String httpProxyHostBackup;
private String httpProxyPortBackup;
Expand Down Expand Up @@ -571,7 +586,7 @@ private void validateXmlContent(File mergedWebXml) throws IOException, MojoExecu
restoreHttpProxy();
}
}

private void validateWithXsd(File mergedWebXml) throws IOException, MojoExecutionException {
try {
setHttpProxyIfNecessary();
Expand All @@ -586,7 +601,7 @@ private void validateWithXsd(File mergedWebXml) throws IOException, MojoExecutio
restoreHttpProxy();
}
}

private void setHttpProxyIfNecessary() {
if (!proxyEnvSet && httpProxyHost != null && !httpProxyHost.isEmpty()) {
proxyEnvSet = true;
Expand All @@ -600,7 +615,7 @@ private void setHttpProxyIfNecessary() {
}
}
}

private void restoreHttpProxy() {
if (proxyEnvSet) {
System.setProperty("http.proxyHost", httpProxyHostBackup != null ? httpProxyHostBackup : "");
Expand All @@ -611,7 +626,7 @@ private void restoreHttpProxy() {
proxyEnvSet = false;
}
}

private StreamSource[] getWebXmlSchema() throws MalformedURLException {
URL webXmlXsdUrl = new URL(webXmlXsdSchema);
return new StreamSource[] {new StreamSource(webXmlXsdUrl.toExternalForm())};
Expand Down Expand Up @@ -651,13 +666,29 @@ private void writeXmlFragments(Path mergedWebXmlPath) throws IOException {
}
}

private <T> T[] arrayAppend(T[] arr, T element) {
final int N = arr.length;
arr = Arrays.copyOf(arr, N + 1);
arr[N] = element;
return arr;
}

private void prepare() {
// For some reason JspC doesn't like it if the dir doesn't
// already exist and refuses to create the web.xml fragment
File generatedSourceDirectoryFile = new File(generatedClasses);
if (!generatedSourceDirectoryFile.exists()) {
generatedSourceDirectoryFile.mkdirs();
}

if (ignoreJspFragmentErrors) {
//Manually add an exclude to jspf
if (excludes == null) {
excludes = new String[]{"**/*.jspf"};
} else {
excludes = arrayAppend(excludes, "**/*.jspf");
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.leonard.maven.plugins.jspc;

import org.apache.maven.plugin.testing.MojoRule;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Test {@link JspcMojo}
*/
public class TestJspcMojoIgnoreJspFragmentsErrors {

@Rule
public MojoRule rule = new MojoRule();

private String PROJECT_PATH = "target/test-classes/unit/project_ignore_jsp_fragments_errors";
private String TARGET_PATH = PROJECT_PATH + "/target/classes/jsp/jsp/";

@Test
public void shouldRespectIncludesAndExcludes() throws Exception {
// Given
File includeExcludeProject = new File(PROJECT_PATH);

// When
rule.executeMojo(includeExcludeProject, "compile");

// Then
Path jspPath = Paths.get(TARGET_PATH + "01_jsp.class");
assertThat(jspPath).exists();

Path jspfPath = Paths.get(TARGET_PATH + "01_jspf.class");
assertThat(jspfPath).doesNotExist();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.leonard.maven.plugins</groupId>
<artifactId>ignore-jsp-fragments-errors-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Test ignore jsp fragments errors</name>

<build>
<plugins>
<plugin>
<groupId>io.leonard.maven.plugins</groupId>
<artifactId>jspc-maven-plugin</artifactId>
<configuration>
<ignoreJspFragmentErrors>true</ignoreJspFragmentErrors>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>

<%
String hello = "01";
%>

<body>
Hello <%=hello %>
</body>

</html>

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>

<%
This file intentionally contains a syntax error. It's excluded by parameter ignoreJspFragmentsErrors so should not compile and therefore the test
should pass anyway.

<body>
Hello <%=hello %>
</body>

</html>

0 comments on commit abd290f

Please sign in to comment.