seems the attachment didn't work..
Pasting the code:

import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.deployer.ArtifactDeployer;
import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
import org.apache.maven.artifact.factory.ArtifactFactory;
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.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

import org.codehaus.plexus.archiver.jar.JarArchiver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


/**
  * @goal installer
 */
public class InstallerMojo extends AbstractMojo {
    /**
     * The Maven Project.
     *
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    protected MavenProject m_project;

    /**
     * Specify the install prefix
     *
     * @parameter expression="${project.groupId}/${project.artifactId}"
     */
    protected String installPrefix;

    /**
     * Specify the database source
     *
     * @parameter default-value="${basedir}/src/main/database"
     */
    protected String databaseSourceDirectory;

    /**
     * Specify the autosys source
     *
     * @parameter default-value="${basedir}/src/main/autosys"
     */
    protected String autosysSourceDirectory;

    /**
     * Specify the runtime source
     *
     * @parameter default-value="${basedir}/src/main/runtime"
     */
    protected String runtimeSourceDirectory;

    /**
     * Specify the database outpur
     *
     * @parameter default-value="${basedir}/target/database"
     */
    protected String m_databaseOutputDirectory;

    /**
     * Specify the autosys output
     *
     * @parameter default-value="${basedir}/target/autosys"
     */
    protected String m_autosysOutputDirectory;

    /**
     * Specify the runtime output
     *
     * @parameter default-value="${basedir}/target/runtime"
     */
    protected String m_runtimeOutputDirectory;

    /**
     * Name of the generated JAR.
     *
     * @parameter alias="jarName"
expression="${project.build.finalName}"
     */
    private String m_finalName;

    /**
     * The Jar archiver.
     *
     * @parameter
expression="${component.org.codehaus.plexus.archiver.Archiver#jar}"
     * @required
     */
    private JarArchiver m_jarArchiver;

    /**
     * @parameter
expression="${component.org.apache.maven.artifact.factory.ArtifactFactor
y}"
     * @required
     * @readonly
     */
    private ArtifactFactory m_artifactFactory;

    /**
     * @parameter
expression="${component.org.apache.maven.artifact.installer.ArtifactInst
aller}"
     * @readonly
     */
    protected ArtifactInstaller m_installer;

    /**
      * @parameter
expression="${component.org.apache.maven.artifact.deployer.ArtifactDeplo
yer}"
      * @required
      * @readonly
      */
    private ArtifactDeployer deployer;

    /**
      * Mode in which the packager is run, either hdev or hpackage
      *
      * @parameter   expression="${mode}"
      */
    protected String m_mode;

    /**
     * @parameter expression="${localRepository}"
     * @readonly
     */
    protected ArtifactRepository m_localRepository;
    protected String m_groupId;
    protected String m_artifactId;
    protected String m_version;
    protected String m_type;

    /**
     * method called upon execution of the plugin
     *
     * @throws MojoExecutionException
     * @throws MojoFailureException
     */
    public void execute() throws MojoExecutionException,
MojoFailureException {
        //Setup
        init();

        // Install the db related artifacts
        installFile(databaseSourceDirectory, m_databaseOutputDirectory,
"db");

    }

    // Initialize the params
    protected void init() {
        m_groupId = m_project.getGroupId();
        m_artifactId = m_project.getArtifactId();
        m_version = m_project.getVersion();
        m_type = m_project.getPackaging();
        installPrefix = installPrefix.replace('.', File.separatorChar);
    }

    /**
     * Installs the jar file in the repository for the artifacts present
in source.
     * The jar is appended with the jarType.
     *
     * @param source
     * @param destination
     * @param jarType
     * @throws MojoExecutionException
     */
    public void installFile(String source, String destination, String
jarType)
        throws MojoExecutionException {
        if (!new File(source).exists()) {
            return;
        }

        //Copy artifacts from source to destination
        getLog().info("Copying files from :" + source + " to " +
destination +
            File.separatorChar + installPrefix);

        try {
            copyFiles(source, destination + File.separatorChar +
installPrefix);
            getLog().info("Copy completed");
        } catch (IOException e) {
            throw new MojoExecutionException("Error copying files from "
+
                source + " to " + destination + e.getMessage(), e);
        }

            //Deploy the jar archive
            deploy(createArchive(destination, jarType), jarType);
     }

    private void deploy(File file, String jarType)
        throws MojoExecutionException {
        if (!file.exists()) {
            throw new MojoExecutionException(file.getPath() + " not
found.");
        }

        // Create the artifact
        Artifact artifact =
m_artifactFactory.createArtifactWithClassifier(m_groupId,
                m_artifactId, m_version, "jar", jarType);

        try {
            deployer.deploy(file, artifact,
                m_project.getDistributionManagementArtifactRepository(),
                m_localRepository);
        } catch (ArtifactDeploymentException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }

    /**
     * Copy files from strPath to dstPath
     *
     * @param strPath
     * @param dstPath
     * @throws IOException
     */
    protected void copyFiles(String strPath, String dstPath)
        throws IOException {
        File src = new File(strPath);
        File dest = new File(dstPath);

        if (!src.getName().equals("CVS")) {
            if (src.isDirectory()) {
                if (!dest.exists()) {
                    dest.mkdirs();
                }

                String[] list = src.list();

                for (int i = 0; i < list.length; i++) {
                    String dest1 = dest.getAbsolutePath() +
File.separatorChar +
                        list[i];
                    String src1 = src.getAbsolutePath() +
File.separatorChar +
                        list[i];
                    copyFiles(src1, dest1);
                }
            } else {
                FileInputStream fin = new FileInputStream(src);
                FileOutputStream fout = new FileOutputStream(dest);
                int c;

                while ((c = fin.read()) >= 0)
                    fout.write(c);

                fin.close();
                fout.close();
            }
        }
    }

    /**
     * Generates the jar file, by archiving the output directory.
     * The jar is named as groupId.artifactId-version-type.jar
     *
     * @param outputDirectory
     * @param type
     * @return jarfile
     * @throws MojoExecutionException
     */
    protected File createArchive(String outputDirectory, String type)
        throws MojoExecutionException {
        MavenArchiveConfiguration archive = new
MavenArchiveConfiguration();

        if ((m_finalName == null) || m_finalName.equals("")) {
            m_finalName = m_groupId + "." + m_artifactId + "-" +
m_version;
        }

        File targetLocation = new File(outputDirectory);
        File jarFile = new File(targetLocation.getParent(),
                m_finalName + "." + type + ".jar");
        getLog().info("Creating jar file:" + jarFile.getAbsolutePath());

        MavenArchiver archiver = new MavenArchiver();

        archiver.setArchiver(m_jarArchiver);

        archiver.setOutputFile(jarFile);

        try {
            File contentDirectory = new File(outputDirectory);

            if (!contentDirectory.exists()) {
                getLog().warn("JAR will be empty - no content was marked
for inclusion!");
            } else {
                archiver.getArchiver().addDirectory(contentDirectory);
            }

            archiver.createArchive(m_project, archive);

            return jarFile;
        } catch (Exception e) {
            throw new MojoExecutionException("Error assembling JAR", e);
        }
    }
}

-----Original Message-----
From: Alok, Niraj 
Sent: Saturday, June 16, 2007 12:34 AM
To: 'Maven Users List'
Subject: RE: 0-Snapshot numbering for classifiers

I need to write another plugin to do this because the sources here are
not java files, but database files which are stored in cvs. 
The directory structure is src/main/database and we want all the files
inside the database folder to be put in the repository for use in
dependencies by other modules. 

To check if there was a problem with the way I wrote the plugin, I also
included sources plugin in the pom and found that app-sources.jar is
getting generated with the same build number as app.jar, but app-db.jar
is getting installed with one number less. This is alteast pointing me
that there's something wrong in my plugin, but cant figure out exactly
what. 

I am attaching the code here also for help.

Thanks!
Niraj

-----Original Message-----
From: Eric Redmond [mailto:[EMAIL PROTECTED] 
Sent: Saturday, June 16, 2007 12:10 AM
To: Maven Users List
Subject: Re: 0-Snapshot numbering for classifiers

Why are you writing code to do this? You should be installing the
sources
via the source plugin:

mvn source:jar

Or assembly plugin:

http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.ht
ml

Eric

On 6/15/07, niraj <[EMAIL PROTECTED]> wrote:
>
>
> To be more specific, I am installing the app.jar through normal mvn
deploy
> and the sources.jar through a custom pojo, as its not sources, but
some
> other project files,  and the code for that is:
>
>         Artifact artifact =
> m_artifactFactory.createArtifactWithClassifier(m_groupId,
>                 m_artifactId, m_version, "jar", jarType);
>
>         try {
>             deployer.deploy(file, artifact,
>
m_project.getDistributionManagementArtifactRepository(),
>                 m_localRepository);
>         } catch (ArtifactDeploymentException e) {
>             throw new MojoExecutionException(e.getMessage(), e);
>         }
>
> While downloading, I am just adding the classifier element, jarType in
the
> dependency tag.
> It works for versioned jars, not for snapshots. I am using mvn 2.
> --
> View this message in context:
>
http://www.nabble.com/0-Snapshot-numbering-for-classifiers-tf3927571s177
.html#a11138790
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
Eric Redmond
http://www.sonatype.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to