From f5f995d78b08dcaf00920e5cc48788932a805cc2 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sat, 30 Jul 2011 14:14:04 +0200 Subject: [PATCH] added AbstractBaseScmMojo --- .../sonia/scm/maven/AbstractBaseScmMojo.java | 380 ++++++++++++++++++ .../main/java/sonia/scm/maven/RunMojo.java | 320 +-------------- 2 files changed, 385 insertions(+), 315 deletions(-) create mode 100644 maven/scm-maven-plugin/src/main/java/sonia/scm/maven/AbstractBaseScmMojo.java diff --git a/maven/scm-maven-plugin/src/main/java/sonia/scm/maven/AbstractBaseScmMojo.java b/maven/scm-maven-plugin/src/main/java/sonia/scm/maven/AbstractBaseScmMojo.java new file mode 100644 index 0000000000..e1cd34f89b --- /dev/null +++ b/maven/scm-maven-plugin/src/main/java/sonia/scm/maven/AbstractBaseScmMojo.java @@ -0,0 +1,380 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.maven; + +//~--- non-JDK imports -------------------------------------------------------- + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.installer.ArtifactInstallationException; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.artifact.resolver.ArtifactNotFoundException; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.plugin.MojoExecutionException; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import javax.xml.bind.JAXB; + +/** + * + * @author Sebastian Sdorra + */ +public abstract class AbstractBaseScmMojo extends AbstractScmMojo +{ + + /** Field description */ + private static final String FILE_CLASSPATH = "classpath.xml"; + + /** Field description */ + private static final String RESOURCE_DEPENDENCY_LIST = + "WEB-INF/classes/config/dependencies.list"; + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param warFile + * + * @return + * + * @throws MojoExecutionException + */ + protected List createExcludeList(File warFile) + throws MojoExecutionException + { + List excludeList = new ArrayList(); + InputStream input = null; + + try + { + JarFile file = new JarFile(warFile); + JarEntry entry = file.getJarEntry(RESOURCE_DEPENDENCY_LIST); + + if (entry == null) + { + throw new MojoExecutionException("could not find dependency list"); + } + + input = file.getInputStream(entry); + + Scanner scanner = null; + + try + { + scanner = new Scanner(input); + + while (scanner.hasNextLine()) + { + parseLine(excludeList, scanner.nextLine()); + } + } + finally + { + if (scanner != null) + { + scanner.close(); + } + } + } + catch (IOException ex) + { + throw new MojoExecutionException("could not read dependency file"); + } + finally + { + IOUtils.closeQuietly(input); + } + + return excludeList; + } + + /** + * Method description + * + * + * + * + * @param excludeList + * @throws MojoExecutionException + */ + protected void installArtifacts(List excludeList) + throws MojoExecutionException + { + File pluginDirectory = new File(scmHome, "plugins"); + + if (!pluginDirectory.exists() &&!pluginDirectory.mkdirs()) + { + throw new MojoExecutionException( + "could not create plugin directory ".concat( + pluginDirectory.getPath())); + } + + String repositoryUrl = "file://".concat(pluginDirectory.getAbsolutePath()); + ArtifactRepositoryLayout layout = + availableRepositoryLayouts.get(repositoryLayout); + + if (layout == null) + { + throw new MojoExecutionException( + "could not find repository layout ".concat(repositoryLayout)); + } + + ArtifactRepository pluginRepository = + artifactRepositoryFactory.createDeploymentArtifactRepository( + "scm-run-plugin", repositoryUrl, layout, true); + List classpath = new ArrayList(); + String pluginDirectoryPath = pluginDirectory.getAbsolutePath(); + + installArtifact(excludeList, pluginDirectoryPath, classpath, + pluginRepository, projectArtifact); + + if (artifacts != null) + { + for (Artifact artifact : artifacts) + { + installArtifact(excludeList, pluginDirectoryPath, classpath, + pluginRepository, artifact); + } + } + + File classpathFile = new File(pluginDirectory, FILE_CLASSPATH); + + if (classpathFile.exists()) + { + readClasspathFile(classpath, classpathFile); + } + + writeClasspathFile(classpath, classpathFile); + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + * + * @throws MojoExecutionException + */ + protected File getWebApplicationArchive() throws MojoExecutionException + { + File warFile = null; + Artifact artifact = artifactFactory.createArtifact(groupId, artifactId, + version, "", type); + + try + { + artifactResolver.resolve(artifact, remoteRepositories, localRepository); + warFile = artifact.getFile(); + + if (!warFile.exists()) + { + throw new MojoExecutionException("could not find webapp artifact file"); + } + } + catch (ArtifactNotFoundException ex) + { + throw new MojoExecutionException("could not find artifact", ex); + } + catch (ArtifactResolutionException ex) + { + throw new MojoExecutionException("could not fetch war-file", ex); + } + + return warFile; + } + + //~--- methods -------------------------------------------------------------- + + /** + * Method description + * + * + * @param pluginDirectoryPath + * @param classpath + * @param source + * @param artifact + * @param localRepository + * + * @throws ArtifactInstallationException + */ + private void install(String pluginDirectoryPath, List classpath, + File source, Artifact artifact, + ArtifactRepository localRepository) + throws ArtifactInstallationException + { + try + { + String localPath = localRepository.pathOf(artifact); + File destination = new File(localRepository.getBasedir(), localPath); + + if (!destination.getParentFile().exists()) + { + destination.getParentFile().mkdirs(); + } + + getLog().info("Installing artifact " + source.getPath() + " to " + + destination); + FileUtils.copyFile(source, destination); + + String relativePath = + destination.getAbsolutePath().substring(pluginDirectoryPath.length()); + + classpath.add(relativePath); + } + catch (IOException e) + { + throw new ArtifactInstallationException("Error installing artifact: " + + e.getMessage(), e); + } + } + + /** + * Method description + * + * + * + * + * @param excludeList + * @param pluginDirectoryPath + * @param classpath + * @param pluginRepository + * @param artifact + * + * @throws MojoExecutionException + */ + private void installArtifact(List excludeList, + String pluginDirectoryPath, + List classpath, + ArtifactRepository pluginRepository, + Artifact artifact) + throws MojoExecutionException + { + String id = + artifact.getGroupId().concat(":").concat(artifact.getArtifactId()); + + if (!excludeList.contains(id)) + { + if (artifact.getFile() != null) + { + try + { + + // See: http://mail-archives.apache.org/mod_mbox/maven-dev/200511.mbox/%3c437288F4.4080003@apache.org%3e + artifact.isSnapshot(); + install(pluginDirectoryPath, classpath, artifact.getFile(), artifact, + pluginRepository); + } + catch (ArtifactInstallationException e) + { + throw new MojoExecutionException("Failed to copy artifact.", e); + } + } + else + { + throw new MojoExecutionException( + "could not find file for ".concat(artifact.getId())); + } + } + } + + /** + * Method description + * + * + * + * @param excludeList + * @param line + */ + private void parseLine(List excludeList, String line) + { + line = line.trim(); + + if (StringUtils.isNotEmpty(line)) + { + String[] parts = line.split(":"); + + if (parts.length >= 2) + { + excludeList.add(parts[0].concat(":").concat(parts[1])); + } + } + } + + /** + * Method description + * + * + * @param classpath + * @param classpathFile + */ + private void readClasspathFile(List classpath, File classpathFile) + { + Classpath c = JAXB.unmarshal(classpathFile, Classpath.class); + + classpath.addAll(c.getClasspathElements()); + } + + /** + * Method description + * + * + * @param classpath + * @param classpathFile + */ + private void writeClasspathFile(List classpath, File classpathFile) + { + Classpath c = new Classpath(); + + c.setClasspathElements(classpath); + JAXB.marshal(c, classpathFile); + } +} diff --git a/maven/scm-maven-plugin/src/main/java/sonia/scm/maven/RunMojo.java b/maven/scm-maven-plugin/src/main/java/sonia/scm/maven/RunMojo.java index 4ee4f33bea..a34f411c7a 100644 --- a/maven/scm-maven-plugin/src/main/java/sonia/scm/maven/RunMojo.java +++ b/maven/scm-maven-plugin/src/main/java/sonia/scm/maven/RunMojo.java @@ -35,15 +35,6 @@ package sonia.scm.maven; //~--- non-JDK imports -------------------------------------------------------- -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.installer.ArtifactInstallationException; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.apache.maven.artifact.resolver.ArtifactNotFoundException; -import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -54,16 +45,8 @@ import org.eclipse.jetty.webapp.WebAppContext; //~--- JDK imports ------------------------------------------------------------ import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; import java.util.List; -import java.util.Scanner; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; - -import javax.xml.bind.JAXB; /** * @@ -72,18 +55,9 @@ import javax.xml.bind.JAXB; * @requiresDependencyResolution runtime * @phase package */ -public class RunMojo extends AbstractScmMojo +public class RunMojo extends AbstractBaseScmMojo { - /** Field description */ - private static final String FILE_CLASSPATH = "classpath.xml"; - - /** Field description */ - private static final String RESOURCE_DEPENDENCY_LIST = - "WEB-INF/classes/config/dependencies.list"; - - //~--- methods -------------------------------------------------------------- - /** * Method description * @@ -94,33 +68,11 @@ public class RunMojo extends AbstractScmMojo @Override public void execute() throws MojoExecutionException, MojoFailureException { - Artifact artifact = artifactFactory.createArtifact(groupId, artifactId, - version, "", type); + File warFile = getWebApplicationArchive(); + List excludeList = createExcludeList(warFile); - try - { - artifactResolver.resolve(artifact, remoteRepositories, localRepository); - - File warFile = artifact.getFile(); - - if (!warFile.exists()) - { - throw new MojoFailureException("could not find webapp artifact file"); - } - - List excludeList = createExcludeList(warFile); - - installArtifacts(excludeList); - runServletContainer(warFile); - } - catch (ArtifactNotFoundException ex) - { - throw new MojoExecutionException("could not fetch war-file", ex); - } - catch (ArtifactResolutionException ex) - { - throw new MojoExecutionException("could not fetch war-file", ex); - } + installArtifacts(excludeList); + runServletContainer(warFile); } //~--- get methods ---------------------------------------------------------- @@ -173,253 +125,6 @@ public class RunMojo extends AbstractScmMojo //~--- methods -------------------------------------------------------------- - /** - * Method description - * - * - * @param warFile - * - * @return - * - * @throws MojoExecutionException - */ - private List createExcludeList(File warFile) - throws MojoExecutionException - { - List excludeList = new ArrayList(); - InputStream input = null; - - try - { - JarFile file = new JarFile(warFile); - JarEntry entry = file.getJarEntry(RESOURCE_DEPENDENCY_LIST); - - if (entry == null) - { - throw new MojoExecutionException("could not find dependency list"); - } - - input = file.getInputStream(entry); - - Scanner scanner = null; - - try - { - scanner = new Scanner(input); - - while (scanner.hasNextLine()) - { - parseLine(excludeList, scanner.nextLine()); - } - } - finally - { - if (scanner != null) - { - scanner.close(); - } - } - } - catch (IOException ex) - { - throw new MojoExecutionException("could not read dependency file"); - } - finally - { - IOUtils.closeQuietly(input); - } - - return excludeList; - } - - /** - * Method description - * - * - * @param pluginDirectoryPath - * @param classpath - * @param source - * @param artifact - * @param localRepository - * - * @throws ArtifactInstallationException - */ - private void install(String pluginDirectoryPath, List classpath, - File source, Artifact artifact, - ArtifactRepository localRepository) - throws ArtifactInstallationException - { - try - { - String localPath = localRepository.pathOf(artifact); - File destination = new File(localRepository.getBasedir(), localPath); - - if (!destination.getParentFile().exists()) - { - destination.getParentFile().mkdirs(); - } - - getLog().info("Installing artifact " + source.getPath() + " to " - + destination); - FileUtils.copyFile(source, destination); - - String relativePath = - destination.getAbsolutePath().substring(pluginDirectoryPath.length()); - - classpath.add(relativePath); - } - catch (IOException e) - { - throw new ArtifactInstallationException("Error installing artifact: " - + e.getMessage(), e); - } - } - - /** - * Method description - * - * - * - * - * @param excludeList - * @param pluginDirectoryPath - * @param classpath - * @param pluginRepository - * @param artifact - * - * @throws MojoExecutionException - */ - private void installArtifact(List excludeList, - String pluginDirectoryPath, - List classpath, - ArtifactRepository pluginRepository, - Artifact artifact) - throws MojoExecutionException - { - String id = - artifact.getGroupId().concat(":").concat(artifact.getArtifactId()); - - if (!excludeList.contains(id)) - { - if (artifact.getFile() != null) - { - try - { - - // See: http://mail-archives.apache.org/mod_mbox/maven-dev/200511.mbox/%3c437288F4.4080003@apache.org%3e - artifact.isSnapshot(); - install(pluginDirectoryPath, classpath, artifact.getFile(), artifact, - pluginRepository); - } - catch (ArtifactInstallationException e) - { - throw new MojoExecutionException("Failed to copy artifact.", e); - } - } - else - { - throw new MojoExecutionException( - "could not find file for ".concat(artifact.getId())); - } - } - } - - /** - * Method description - * - * - * - * - * @param excludeList - * @throws MojoExecutionException - */ - private void installArtifacts(List excludeList) - throws MojoExecutionException - { - File pluginDirectory = new File(scmHome, "plugins"); - - if (!pluginDirectory.exists() &&!pluginDirectory.mkdirs()) - { - throw new MojoExecutionException( - "could not create plugin directory ".concat( - pluginDirectory.getPath())); - } - - String repositoryUrl = "file://".concat(pluginDirectory.getAbsolutePath()); - ArtifactRepositoryLayout layout = - availableRepositoryLayouts.get(repositoryLayout); - - if (layout == null) - { - throw new MojoExecutionException( - "could not find repository layout ".concat(repositoryLayout)); - } - - ArtifactRepository pluginRepository = - artifactRepositoryFactory.createDeploymentArtifactRepository( - "scm-run-plugin", repositoryUrl, layout, true); - List classpath = new ArrayList(); - String pluginDirectoryPath = pluginDirectory.getAbsolutePath(); - - installArtifact(excludeList, pluginDirectoryPath, classpath, - pluginRepository, projectArtifact); - - if (artifacts != null) - { - for (Artifact artifact : artifacts) - { - installArtifact(excludeList, pluginDirectoryPath, classpath, - pluginRepository, artifact); - } - } - - File classpathFile = new File(pluginDirectory, FILE_CLASSPATH); - - if (classpathFile.exists()) - { - readClasspathFile(classpath, classpathFile); - } - - writeClasspathFile(classpath, classpathFile); - } - - /** - * Method description - * - * - * - * @param excludeList - * @param line - */ - private void parseLine(List excludeList, String line) - { - line = line.trim(); - - if (StringUtils.isNotEmpty(line)) - { - String[] parts = line.split(":"); - - if (parts.length >= 2) - { - excludeList.add(parts[0].concat(":").concat(parts[1])); - } - } - } - - /** - * Method description - * - * - * @param classpath - * @param classpathFile - */ - private void readClasspathFile(List classpath, File classpathFile) - { - Classpath c = JAXB.unmarshal(classpathFile, Classpath.class); - - classpath.addAll(c.getClasspathElements()); - } - /** * Method description * @@ -457,21 +162,6 @@ public class RunMojo extends AbstractScmMojo } } - /** - * Method description - * - * - * @param classpath - * @param classpathFile - */ - private void writeClasspathFile(List classpath, File classpathFile) - { - Classpath c = new Classpath(); - - c.setClasspathElements(classpath); - JAXB.marshal(c, classpathFile); - } - //~--- fields --------------------------------------------------------------- /**