Author: olamy
Date: Fri Apr 13 17:08:18 2012
New Revision: 1325842
URL: http://svn.apache.org/viewvc?rev=1325842&view=rev
Log:
[MTOMCAT-142] contextFile path="..." ignored when using tomcat7:run
Modified:
tomcat/maven-plugin/trunk/tomcat7-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat7/run/AbstractRunMojo.java
Modified:
tomcat/maven-plugin/trunk/tomcat7-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat7/run/AbstractRunMojo.java
URL:
http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/tomcat7-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat7/run/AbstractRunMojo.java?rev=1325842&r1=1325841&r2=1325842&view=diff
==============================================================================
---
tomcat/maven-plugin/trunk/tomcat7-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat7/run/AbstractRunMojo.java
(original)
+++
tomcat/maven-plugin/trunk/tomcat7-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat7/run/AbstractRunMojo.java
Fri Apr 13 17:08:18 2012
@@ -23,6 +23,7 @@ import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
+import org.apache.catalina.core.StandardContext;
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.realm.MemoryRealm;
import org.apache.catalina.servlets.DefaultServlet;
@@ -30,6 +31,7 @@ import org.apache.catalina.startup.Catal
import org.apache.catalina.startup.CatalinaProperties;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.AccessLogValve;
+import org.apache.commons.lang.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.plugin.MojoExecutionException;
@@ -59,7 +61,12 @@ import javax.servlet.ServletException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
@@ -457,14 +464,31 @@ public abstract class AbstractRunMojo
{
String contextPath = getPath();
- Context context =
- container.addWebapp( "/".equals( contextPath ) ? "" : contextPath,
getDocBase().getAbsolutePath() );
+ contextPath = "/".equals( contextPath ) ? "" : contextPath;
+
+ String baseDir = getDocBase().getAbsolutePath();
+
+ File overridedContextFile = getContextFile();
+
+ if ( overridedContextFile != null && overridedContextFile.exists() )
+ {
+ StandardContext standardContext = parseContextFile(
overridedContextFile );
+
+ if ( standardContext.getPath() != null )
+ {
+ contextPath = standardContext.getPath();
+ }
+ if ( standardContext.getDocBase() != null )
+ {
+ baseDir = standardContext.getDocBase();
+ }
+ }
+
+ Context context = container.addWebapp( contextPath, baseDir );
context.setResources(
new MyDirContext( new File(
project.getBuild().getOutputDirectory() ).getAbsolutePath() ) );
- //Tomcat.initWebappDefaults( context );
-
if ( useSeparateTomcatClassLoader )
{
context.setParentClassLoader( getTomcatClassLoader() );
@@ -474,10 +498,9 @@ public abstract class AbstractRunMojo
context.setLoader( loader );
- File contextFile = getContextFile();
- if ( contextFile != null )
+ if ( overridedContextFile != null )
{
- context.setConfigFile( getContextFile().toURI().toURL() );
+ context.setConfigFile( overridedContextFile.toURI().toURL() );
}
if ( classLoaderClass != null )
@@ -489,6 +512,52 @@ public abstract class AbstractRunMojo
}
+ protected StandardContext parseContextFile( File file )
+ throws MojoExecutionException
+ {
+ try
+ {
+ StandardContext standardContext = new StandardContext();
+ XMLStreamReader reader =
XMLInputFactory.newFactory().createXMLStreamReader( new FileInputStream( file )
);
+
+ int tag = reader.next();
+
+ while ( true )
+ {
+ if ( tag == XMLStreamConstants.START_ELEMENT &&
StringUtils.equals( "Context", reader.getLocalName() ) )
+ {
+ String path = reader.getAttributeValue( null, "path" );
+ if ( StringUtils.isNotBlank( path ) )
+ {
+ standardContext.setPath( path );
+ }
+
+ String docBase = reader.getAttributeValue( null, "docBase"
);
+ if ( StringUtils.isNotBlank( docBase ) )
+ {
+ standardContext.setDocBase( docBase );
+ }
+ }
+ if ( !reader.hasNext() )
+ {
+ break;
+ }
+ tag = reader.next();
+ }
+
+ return standardContext;
+ }
+ catch ( XMLStreamException e )
+ {
+ throw new MojoExecutionException( e.getMessage(), e );
+ }
+ catch ( FileNotFoundException e )
+ {
+ throw new MojoExecutionException( e.getMessage(), e );
+ }
+ }
+
+
private static class MyDirContext
extends FileDirContext
{
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]