Commit 1e1430fb authored by rgauss's avatar rgauss
Browse files

- Added attachClasses configuration which will attach a JAR with as separate 'classes' artifact

- Added attachConfig configuration which attaches a 'config' classifier JAR with the contents of the config dir of the AMP, useful as a test dependency

Fixes issue 147

git-svn-id: http://maven-alfresco-archetypes.googlecode.com/svn/trunk@706 04253f4f-3451-0410-a141-5562f1e59037
parent 427944a0
Loading
Loading
Loading
Loading
+81 −13
Original line number Diff line number Diff line
@@ -73,6 +73,20 @@ public class AmpMojo extends AbstractMojo {
     */
    protected boolean includeDependencies;
    
    /**
     * Whether the JAR produced should be attached as a separate 'classes' artifact.
     * 
     * @parameter property="maven.alfresco.attachClasses" default-value="false"
     */
    protected boolean attachClasses;
    
    /**
     * Whether a config artifact should be produced should be attached as a separate 'config' artifact.
     * 
     * @parameter property="maven.alfresco.attachConfig" default-value="false"
     */
    protected boolean attachConfig;

    /**
     * Directory of the final generated AMP
     *
@@ -128,21 +142,31 @@ public class AmpMojo extends AbstractMojo {
        	gatherDependencies();
        }
        
        File jarFile = createJarArchive();
        if (this.attachClasses) {
            this.projectHelper.attachArtifact(this.project, "jar", "classes", jarFile);
        }

        File ampFile = createArchive();
        if (this.classifier != null) {
            this.projectHelper.attachArtifact(this.project, "amp", this.classifier, ampFile);
        } else {
            this.project.getArtifact().setFile(ampFile);
        }
        
        if (attachConfig) {
            File configFile = createConfigArchive();
            this.projectHelper.attachArtifact(this.project, "jar", "config", configFile);
        }
    }

    /**
     * Creates and returns the AMP archive, invoking the AmpArchiver
     * Creates and returns the JAR archive, invoking the MavenArchiver
     *
     * @return a File pointing to an existing AMP package, contained
     * @return a File pointing to the JAR, contained
     *         in ${project.build.outputDirectory}
     */
    protected File createArchive()
    protected File createJarArchive()
            throws MojoExecutionException {
        File jarFile = getFile(
                new File(this.ampBuildDirectory, AmpModel.AMP_FOLDER_LIB),
@@ -150,6 +174,29 @@ public class AmpMojo extends AbstractMojo {
                this.classifier,
                "jar");
        
        MavenArchiver jarArchiver = new MavenArchiver();
        jarArchiver.setArchiver(new JarArchiver());
        jarArchiver.setOutputFile(jarFile);
        
        try {
            jarArchiver.getArchiver().addDirectory(this.classesDirectory, new String[] {}, new String[] {});
            jarArchiver.createArchive(this.session, this.project, this.archive);
            return jarFile;
        }
        catch (Exception e) {
            throw new MojoExecutionException("Error creating JAR", e);
        }
    }
    
    /**
     * Creates and returns the AMP archive, invoking the AmpArchiver
     *
     * @return a File pointing to an existing AMP package, contained
     *         in ${project.build.outputDirectory}
     */
    protected File createArchive()
            throws MojoExecutionException {

        File ampFile = getFile(
                this.ampFinalDir,
                this.ampFinalName,
@@ -157,10 +204,6 @@ public class AmpMojo extends AbstractMojo {
                "amp"
        );

        MavenArchiver jarArchiver = new MavenArchiver();
        jarArchiver.setArchiver(new JarArchiver());
        jarArchiver.setOutputFile(jarFile);

        MavenArchiver ampArchiver = new MavenArchiver();
        ampArchiver.setArchiver(new AmpArchiver());
        ampArchiver.setOutputFile(ampFile);
@@ -172,12 +215,6 @@ public class AmpMojo extends AbstractMojo {
        if (!this.ampBuildDirectory.exists()) {
            getLog().warn("ampBuildDirectory does not exist - AMP will be empty");
        } else {
            try {
                jarArchiver.getArchiver().addDirectory(this.classesDirectory, new String[]{}, new String[]{});
                jarArchiver.createArchive(this.session, this.project, this.archive);
              } catch (Exception e) {
                    throw new MojoExecutionException("Error creating JAR", e);
              }
              try {
                ampArchiver.getArchiver().addDirectory(this.ampBuildDirectory, new String[]{"**"}, new String[]{});
                ampArchiver.createArchive(this.session, this.project, this.archive);
@@ -189,6 +226,37 @@ public class AmpMojo extends AbstractMojo {
        return ampFile;
    }
    
    /**
     * Creates and returns the config archive, invoking the MavenArchiver
     *
     * @return a File pointing to the JAR, contained
     *         in ${project.build.outputDirectory}
     */
    protected File createConfigArchive()
            throws MojoExecutionException {
        File configFile = getFile(
                new File(this.project.getBuild().getDirectory()),
                this.ampFinalName,
                this.classifier,
                "jar");
        
        MavenArchiver configArchiver = new MavenArchiver();
        configArchiver.setArchiver(new JarArchiver());
        configArchiver.setOutputFile(configFile);
        
        try {
            configArchiver.getArchiver().addDirectory(
                    new File(this.ampBuildDirectory, "config"), 
                    new String[] { }, 
                    new String[] { "**/*.class" });
            configArchiver.createArchive(this.session, this.project, this.archive);
            return configFile;
        }
        catch (Exception e) {
            throw new MojoExecutionException("Error creating config artifact", e);
        }
    }

    /**
     * Builds a File object pointing to the target AMP package; the pointer to the File is created taking into
     * account the (optional) artifact classifier defined