Author: mfortun Date: 2011-08-26 10:56:14 +0200 (Fri, 26 Aug 2011) New Revision: 1185 Url: http://nuiton.org/repositories/revision/wikitty/1185 Log: *Now load dependency from pom if needed Added: trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/AbstractWPLoadDependencyMojo.java trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/CopyDependencyUtil.java trunk/wp-maven-plugin/src/main/resources/log4j.properties Modified: trunk/wp-maven-plugin/pom.xml trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/AbstractWPMojo.java trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPDeployJarMojo.java trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPDeployMojo.java trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPInitMojo.java trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPJarMojo.java trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPRunMojo.java trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPUpdateMojo.java Modified: trunk/wp-maven-plugin/pom.xml =================================================================== --- trunk/wp-maven-plugin/pom.xml 2011-08-25 09:00:58 UTC (rev 1184) +++ trunk/wp-maven-plugin/pom.xml 2011-08-26 08:56:14 UTC (rev 1185) @@ -87,6 +87,10 @@ <artifactId>maven-site-plugin</artifactId> </dependency> + <dependency> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + </dependency> </dependencies> Added: trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/AbstractWPLoadDependencyMojo.java =================================================================== --- trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/AbstractWPLoadDependencyMojo.java (rev 0) +++ trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/AbstractWPLoadDependencyMojo.java 2011-08-26 08:56:14 UTC (rev 1185) @@ -0,0 +1,67 @@ +package org.nuiton.wikitty.plugin; + +import java.io.File; +import org.apache.maven.artifact.installer.ArtifactInstaller; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; + + +/** + * This mojo is for init for the goal that need to construct the application + * this mojo, with init will read pom file to load dependency declared + * and so needed by the wikitty publication application project. + * This will copy those needed dependency to the right directory: + * src/main/resources/jar + * + * @author mfortun + * @requiresProject true + */ +public abstract class AbstractWPLoadDependencyMojo extends AbstractWPMojo { + + + /** + * @component + */ + protected ArtifactInstaller installer; + + /** + * @component + */ + protected ArtifactRepositoryFactory repositoryFactory; + + + /** + * Used to look up Artifacts in the remote repository. + * + * @component + */ + protected org.apache.maven.artifact.factory.ArtifactFactory factory; + + + + @Override + protected void init() throws Exception { + + CopyDependencyUtil cpDep = new CopyDependencyUtil(); + + // construct path to jar dir + File basedir = getProject().getBasedir(); + File jarDir = new File(basedir.getAbsolutePath() + File.separator + + SRC_DIR_NAME + File.separator + + MAIN_DIR_NAME + File.separator + + RESOURCES_DIR_NAME + File.separator + + JAR_RESOURCES_DIR_NAME); + + // initialize param for dependency copy + cpDep.setRepositoryFactory(repositoryFactory); + cpDep.setInstaller(installer); + cpDep.setProject(project); + cpDep.setOutputDirectory(jarDir); + cpDep.setFactory(factory); + cpDep.setLog(getLog()); + // run + cpDep.execute(); + } + + + +} Modified: trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/AbstractWPMojo.java =================================================================== --- trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/AbstractWPMojo.java 2011-08-25 09:00:58 UTC (rev 1184) +++ trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/AbstractWPMojo.java 2011-08-26 08:56:14 UTC (rev 1185) @@ -37,7 +37,10 @@ static public String SRC_DIR_NAME = "src"; static public String MAIN_DIR_NAME = "main"; - + static public String APPLICATION_DIR_NAME = "wp"; + static public String RESOURCES_DIR_NAME = "resources"; + static public String IMAGES_RESOURCES_DIR_NAME = "images"; + static public String JAR_RESOURCES_DIR_NAME = "jar"; /** * Project. * Added: trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/CopyDependencyUtil.java =================================================================== --- trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/CopyDependencyUtil.java (rev 0) +++ trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/CopyDependencyUtil.java 2011-08-26 08:56:14 UTC (rev 1185) @@ -0,0 +1,248 @@ +package org.nuiton.wikitty.plugin; + +import java.io.File; +import java.net.MalformedURLException; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.installer.ArtifactInstallationException; +import org.apache.maven.artifact.installer.ArtifactInstaller; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.dependency.AbstractFromDependenciesMojo; +import org.apache.maven.plugin.dependency.utils.DependencyStatusSets; +import org.apache.maven.plugin.dependency.utils.DependencyUtil; +import org.apache.maven.plugin.dependency.utils.filters.DestFileFilter; +import org.apache.maven.project.MavenProject; +import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter; + +public class CopyDependencyUtil extends AbstractFromDependenciesMojo { + + public ArtifactInstaller getInstaller() { + return installer; + } + + public void setInstaller(ArtifactInstaller installer) { + this.installer = installer; + } + + public ArtifactRepositoryFactory getRepositoryFactory() { + return repositoryFactory; + } + + public void setRepositoryFactory(ArtifactRepositoryFactory repositoryFactory) { + this.repositoryFactory = repositoryFactory; + } + + public Map getRepositoryLayouts() { + return repositoryLayouts; + } + + public void setRepositoryLayouts(Map repositoryLayouts) { + this.repositoryLayouts = repositoryLayouts; + } + + /** + * @component + */ + protected ArtifactInstaller installer; + + /** + * @component + */ + protected ArtifactRepositoryFactory repositoryFactory; + + /** + * @component role= + * "org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout" + */ + private Map repositoryLayouts; + + /** + * Main entry into mojo. Gets the list of dependencies and iterates through + * calling copyArtifact. + * + * @throws MojoExecutionException + * with a message if an error occurs. + * + * @see #getDependencies + * @see #copyArtifact(Artifact, boolean) + */ + public void execute() throws MojoExecutionException { + DependencyStatusSets dss = getDependencySets(this.failOnMissingClassifierArtifact); + Set artifacts = dss.getResolvedDependencies(); + + if (!useRepositoryLayout) { + for (Iterator i = artifacts.iterator(); i.hasNext();) { + copyArtifact((Artifact) i.next(), this.stripVersion, + this.prependGroupId); + } + } else { + try { + ArtifactRepository targetRepository = repositoryFactory + .createDeploymentArtifactRepository("local", + outputDirectory.toURL().toExternalForm(), + (ArtifactRepositoryLayout) repositoryLayouts + .get("default"), false /* uniqueVersion */); + for (Iterator i = artifacts.iterator(); i.hasNext();) { + installArtifact((Artifact) i.next(), targetRepository); + } + } catch (MalformedURLException e) { + throw new MojoExecutionException( + "Could not create outputDirectory repository", e); + } + } + + Set skippedArtifacts = dss.getSkippedDependencies(); + for (Iterator i = skippedArtifacts.iterator(); i.hasNext();) { + Artifact artifact = (Artifact) i.next(); + getLog().info( + artifact.getFile().getName() + + " already exists in destination."); + } + + if (isCopyPom()) { + copyPoms(getOutputDirectory(), artifacts, this.stripVersion); + copyPoms(getOutputDirectory(), skippedArtifacts, this.stripVersion); // Artifacts + // that + // already + // exist + // may + // not + // already + // have + // poms. + } + } + + private void installArtifact(Artifact artifact, + ArtifactRepository targetRepository) { + try { + if ("pom".equals(artifact.getType())) { + installer.install(artifact.getFile(), artifact, + targetRepository); + installBaseSnapshot(artifact, targetRepository); + } else { + installer.install(artifact.getFile(), artifact, + targetRepository); + installBaseSnapshot(artifact, targetRepository); + + if (isCopyPom()) { + Artifact pomArtifact = getResolvedPomArtifact(artifact); + if (pomArtifact.getFile() != null + && pomArtifact.getFile().exists()) { + installer.install(pomArtifact.getFile(), pomArtifact, + targetRepository); + installBaseSnapshot(pomArtifact, targetRepository); + } + } + } + } catch (ArtifactInstallationException e) { + getLog().info(e.getMessage()); + } + } + + private void installBaseSnapshot(Artifact artifact, + ArtifactRepository targetRepository) + throws ArtifactInstallationException { + if (artifact.isSnapshot() + && !artifact.getBaseVersion().equals(artifact.getVersion())) { + Artifact baseArtifact = this.factory.createArtifact( + artifact.getGroupId(), artifact.getArtifactId(), + artifact.getBaseVersion(), artifact.getScope(), + artifact.getType()); + installer.install(artifact.getFile(), baseArtifact, + targetRepository); + } + } + + /** + * Copies the Artifact after building the destination file name if + * overridden. This method also checks if the classifier is set and adds it + * to the destination file name if needed. + * + * @param artifact + * representing the object to be copied. + * @param removeVersion + * specifies if the version should be removed from the file name + * when copying. + * @param prependGroupId + * specifies if the groupId should be prepend to the file while + * copying. + * @throws MojoExecutionException + * with a message if an error occurs. + * + * @see DependencyUtil#copyFile(File, File, Log) + * @see DependencyUtil#getFormattedFileName(Artifact, boolean) + */ + protected void copyArtifact(Artifact artifact, boolean removeVersion, + boolean prependGroupId) throws MojoExecutionException { + + String destFileName = DependencyUtil.getFormattedFileName(artifact, + removeVersion, prependGroupId); + + File destDir; + destDir = DependencyUtil.getFormattedOutputDirectory( + useSubDirectoryPerScope, useSubDirectoryPerType, + useSubDirectoryPerArtifact, useRepositoryLayout, stripVersion, + outputDirectory, artifact); + File destFile = new File(destDir, destFileName); + + copyFile(artifact.getFile(), destFile); + } + + /** + * Copy the pom files associated with the artifacts. + */ + public void copyPoms(File destDir, Set artifacts, boolean removeVersion) + throws MojoExecutionException + + { + Iterator iter = artifacts.iterator(); + while (iter.hasNext()) { + Artifact artifact = (Artifact) iter.next(); + Artifact pomArtifact = getResolvedPomArtifact(artifact); + + // Copy the pom + if (pomArtifact.getFile() != null && pomArtifact.getFile().exists()) { + File pomDestFile = new File(destDir, + DependencyUtil.getFormattedFileName(pomArtifact, + removeVersion, prependGroupId)); + if (!pomDestFile.exists()) { + copyFile(pomArtifact.getFile(), pomDestFile); + } + } + } + } + + protected Artifact getResolvedPomArtifact(Artifact artifact) { + Artifact pomArtifact = this.factory.createArtifact( + artifact.getGroupId(), artifact.getArtifactId(), + artifact.getVersion(), "", "pom"); + // Resolve the pom artifact using repos + try { + this.resolver.resolve(pomArtifact, this.remoteRepos, + this.getLocal()); + } catch (Exception e) { + getLog().info(e.getMessage()); + } + return pomArtifact; + } + + protected ArtifactsFilter getMarkedArtifactFilter() { + return new DestFileFilter(this.overWriteReleases, + this.overWriteSnapshots, this.overWriteIfNewer, + this.useSubDirectoryPerArtifact, this.useSubDirectoryPerType, + this.useSubDirectoryPerScope, this.useRepositoryLayout, + this.stripVersion, this.outputDirectory); + } + + public void setProject(MavenProject pro) { + project = pro; + } + +} Modified: trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPDeployJarMojo.java =================================================================== --- trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPDeployJarMojo.java 2011-08-25 09:00:58 UTC (rev 1184) +++ trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPDeployJarMojo.java 2011-08-26 08:56:14 UTC (rev 1185) @@ -88,7 +88,7 @@ * @requiresDependencyResolution runtime * @since 3.2 */ -public class WPDeployJarMojo extends AbstractWPMojo implements Contextualizable { +public class WPDeployJarMojo extends AbstractWPLoadDependencyMojo implements Contextualizable { /** * Id of server. @@ -179,7 +179,7 @@ @Override protected void init() throws Exception { - + super.init(); Modified: trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPDeployMojo.java =================================================================== --- trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPDeployMojo.java 2011-08-25 09:00:58 UTC (rev 1184) +++ trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPDeployMojo.java 2011-08-26 08:56:14 UTC (rev 1185) @@ -39,10 +39,11 @@ * @requiresDependencyResolution runtime * @since 3.2 */ -public class WPDeployMojo extends AbstractWPMojo { +public class WPDeployMojo extends AbstractWPLoadDependencyMojo { @Override protected void init() throws Exception { + super.init(); // TODO } Modified: trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPInitMojo.java =================================================================== --- trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPInitMojo.java 2011-08-25 09:00:58 UTC (rev 1184) +++ trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPInitMojo.java 2011-08-26 08:56:14 UTC (rev 1185) @@ -41,11 +41,6 @@ public class WPInitMojo extends AbstractWPMojo { - static public String APPLICATION_DIR_NAME = "wp"; - static public String RESSOURCES_DIR_NAME = "ressources"; - static public String IMAGES_RESSOURCES_DIR_NAME = "images"; - static public String JAR_RESSOURCES_DIR_NAME = "jar"; - @Override protected void init() throws Exception { // TODO @@ -66,11 +61,11 @@ File appDir = new File(mainDir.getAbsolutePath() + File.separator + APPLICATION_DIR_NAME); File ressourceDir = new File(mainDir.getAbsolutePath() + File.separator - + RESSOURCES_DIR_NAME); + + RESOURCES_DIR_NAME); File imgDir = new File(ressourceDir.getAbsolutePath() + File.separator - + IMAGES_RESSOURCES_DIR_NAME); + + IMAGES_RESOURCES_DIR_NAME); File jarDir = new File(ressourceDir.getAbsolutePath() + File.separator - + JAR_RESSOURCES_DIR_NAME); + + JAR_RESOURCES_DIR_NAME); createDirectoryIfNecessary(srcDir); createDirectoryIfNecessary(mainDir); Modified: trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPJarMojo.java =================================================================== --- trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPJarMojo.java 2011-08-25 09:00:58 UTC (rev 1184) +++ trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPJarMojo.java 2011-08-26 08:56:14 UTC (rev 1185) @@ -47,12 +47,13 @@ * @requiresDependencyResolution runtime * @since 3.2 */ -public class WPJarMojo extends AbstractWPMojo { +public class WPJarMojo extends AbstractWPLoadDependencyMojo { static public String EXTERNALIZE_PREFIX = "externalize-"; @Override protected void init() throws Exception { + super.init(); // TODO } Modified: trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPRunMojo.java =================================================================== --- trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPRunMojo.java 2011-08-25 09:00:58 UTC (rev 1184) +++ trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPRunMojo.java 2011-08-26 08:56:14 UTC (rev 1185) @@ -53,7 +53,7 @@ * @requiresDependencyResolution runtime * @since 3.2 */ -public class WPRunMojo extends AbstractWPMojo { +public class WPRunMojo extends AbstractWPLoadDependencyMojo { private static final String DEPENDCY_TYPE_WAR = "war"; @@ -110,6 +110,7 @@ @Override protected void init() throws Exception { + super.init(); // TODO } Modified: trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPUpdateMojo.java =================================================================== --- trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPUpdateMojo.java 2011-08-25 09:00:58 UTC (rev 1184) +++ trunk/wp-maven-plugin/src/main/java/org/nuiton/wikitty/plugin/WPUpdateMojo.java 2011-08-26 08:56:14 UTC (rev 1185) @@ -39,10 +39,11 @@ * @requiresDependencyResolution runtime * @since 3.2 */ -public class WPUpdateMojo extends AbstractWPMojo { +public class WPUpdateMojo extends AbstractWPLoadDependencyMojo { @Override protected void init() throws Exception { + super.init(); //TODO } Added: trunk/wp-maven-plugin/src/main/resources/log4j.properties =================================================================== --- trunk/wp-maven-plugin/src/main/resources/log4j.properties (rev 0) +++ trunk/wp-maven-plugin/src/main/resources/log4j.properties 2011-08-26 08:56:14 UTC (rev 1185) @@ -0,0 +1,12 @@ +# Global logging configuration +log4j.rootLogger=FATAL, stdout + +# Console output... +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d %5p [%t] (%F:%L) %M - %m%n + +# package level +log4j.logger.org.nuiton.wikitty.publication=DEBUG +#log4j.logger.org.nuiton.util.TimeLog=WARN +log4j.logger.org.apache.struts2.dispatcher.mapper=DEBUG