/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tuscany.tools.sca.web.junit.plugin;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
/**
* @version $Rev$ $Date$
* @goal generate
* @phase process-resources
* @requiresDependencyResolution runtime
* @description Generate the web.xml and geronimo-web.xml
*/
public class WebJUnitGeneratorMojo extends AbstractMojo {
private final static String ASL_HEADER =
"" + "\n";
private final static String GERONIMO_WEB_XML =
ASL_HEADER + "\n"
// + "\n ${context.root}"
+ "\n "
+ "\n "
+ "\n ${groupId}"
+ "\n ${artifactId}"
+ "\n ${version}"
+ "\n war"
+ "\n "
+ "\n "
+ "\n "
+ "\n\n";
private final static String WEB_XML =
ASL_HEADER + "\n"
+ "\n"
+ "\n ${display-name}"
+ "\n "
+ "\n tuscany"
+ "\n org.apache.tuscany.sca.host.webapp.TuscanyServletFilter"
+ "\n "
+ "\n "
+ "\n junit"
+ "\n org.apache.tuscany.sca.host.webapp.junit.JUnitServletFilter"
+ "\n "
+ "\n junit.tests.path"
+ "\n ${junit.tests.path}"
+ "\n "
+ "\n "
+ "\n "
+ "\n tuscany"
+ "\n /*"
+ "\n "
+ "\n "
+ "\n junit"
+ "\n /junit/*"
+ "\n "
+ "\n\n";
/**
* @parameter
*/
private String testsPath;
/**
* @parameter
*/
private boolean geronimo;
/**
* The project to create a build for.
*
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
public void execute() throws MojoExecutionException {
File base =
new File(project.getBasedir(), "target" + File.separator
+ project.getBuild().getFinalName()
+ File.separator
+ "WEB-INF");
base.mkdirs();
// Create the dir to work around the complaint from maven-war-plugin on non-existent folders
new File(project.getBasedir(), "target/classes/META-INF".replace('/', File.separatorChar)).mkdirs();
File webxml = new File(base, "web.xml");
getLog().info("Generating " + webxml.toString());
String name = project.getName();
if (name == null) {
name = project.getGroupId() + "-" + project.getArtifactId();
}
String content = setParameter(WEB_XML, "display-name", name);
if (testsPath == null) {
testsPath = "/WEB-INF/classes/";
}
content = setParameter(content, "junit.tests.path", testsPath);
try {
FileWriter writer = new FileWriter(webxml);
writer.append(content);
writer.close();
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
if (geronimo) {
File geronimoxml = new File(base, "geronimo-web.xml");
getLog().info("Generating " + geronimoxml.toString());
content = setParameter(GERONIMO_WEB_XML, "groupId", project.getGroupId());
content = setParameter(content, "artifactId", project.getArtifactId());
content = setParameter(content, "version", project.getVersion());
// content = setParameter(content, "context.root", "/" + project.getBuild().getFinalName());
try {
geronimoxml.getParentFile().mkdirs();
FileWriter writer = new FileWriter(geronimoxml);
writer.append(content);
writer.close();
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
// Workaround: maven-war-plugin doesn't like non-existing folders
// create target/test-classes
new File(project.getBasedir(), "target" + File.separator + "test-classes").mkdirs();
}
private String setParameter(String xml, String name, String value) {
String pattern = "${" + name + "}";
int index = xml.indexOf(pattern);
if (index != -1) {
String content = xml.substring(0, index) + value + xml.substring(index + pattern.length());
return content;
}
return xml;
}
}