com.spideo.labs.SimpleAssemblyMojo.java Source code

Java tutorial

Introduction

Here is the source code for com.spideo.labs.SimpleAssemblyMojo.java

Source

package com.spideo.labs;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed 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.
 */

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.zip.ZipArchiver;

import java.io.*;
import java.util.Collection;

import org.codehaus.plexus.archiver.jar.Manifest.Attribute;
import org.codehaus.plexus.archiver.jar.Manifest;

import static org.apache.commons.io.filefilter.TrueFileFilter.INSTANCE;

/**
 * Goal which touches a timestamp file.
 *
 * @goal touch
 * @phase process-sources
 */
public class SimpleAssemblyMojo extends AbstractMojo {

    /**
     * Location of the file.
     *
     * @parameter property="project.build.directory"
     * @required
     */
    private File outputDirectory;

    /**
     * The name of the generated aar.
     *
     * @parameter property="project.build.finalName"
     * @required
     */
    private String jarName;

    /**
     * Any Object to print out.
     *
     * @parameter property="echo.message" default-value="Hello World..."
     */
    private Object message;

    /**
     * Any Object to print out.
     *
     * @parameter property="echo.mainClass" default-value=""
     */
    private String mainClass;

    /**
     * The Zip archiver.
     * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="zip"
     */
    private ZipArchiver zipArchiver;

    private static final String BUILD_DIRECTORY = "target/classes";
    private static final String DEPENDENCIES_DIRECTORY = "target/dependencies/lib";

    File buildDirectory = new File(BUILD_DIRECTORY);
    File dependenciesDirectory = new File(DEPENDENCIES_DIRECTORY);

    /**
     * Any Object to print out.
     *
     * @parameter property="echo.packageName" default-value=""
     */
    private String packageName;

    public void execute() throws MojoExecutionException {

        getLog().info("Start");

        try {
            File jarFile = new File("target/jarArchiveNonCompressed.jar");

            JarArchiver archiver = new JarArchiver();
            archiver.setDestFile(jarFile);
            archiver.setCompress(false);
            archiver.addDirectory(buildDirectory);
            archiver.addDirectory(dependenciesDirectory);

            Manifest manifest = new Manifest();
            manifest.addConfiguredAttribute(new Attribute("Bundle-ManifestVersion", "2"));
            manifest.addConfiguredAttribute(new Attribute("Archiver-Version", "Plexus Archiver"));
            manifest.addConfiguredAttribute(new Attribute("Class-Path", getLibrariesName()));
            manifest.addConfiguredAttribute(new Attribute("Main-Class", mainClass));

            archiver.addConfiguredManifest(manifest);

            archiver.createArchive();

        } catch (Exception e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }

    public String getLibrariesName() {

        String libraries = " . ";

        Collection<File> files = FileUtils.listFiles(dependenciesDirectory, INSTANCE, INSTANCE);

        for (File file : files) {
            libraries += file.getName() + " ";
        }

        return libraries;
    }
}