Java tutorial
/** * Copyright 2012 Alex Jones * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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. * */ package uk.co.unclealex.executable.plugin; import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectHelper; import uk.co.unclealex.executable.generator.CodeGeneratorModule; import uk.co.unclealex.executable.generator.Scripter; import uk.co.unclealex.executable.generator.scan.exception.ExecutableScanException; import com.google.common.base.Function; import com.google.common.collect.Iterables; import com.google.inject.Guice; /** * A mojo that generates all the code required by the executable framework. * @goal executable * @phase package * @requiresDependencyResolution runtime * @author alex * */ public class ExecutableMojo extends AbstractMojo { /** * This is where build results go. * * @parameter default-value="${project.build.directory}" * @required * @readonly */ private File buildDirectory; /** * The Maven project. * * @parameter expression="${project}" * @required * @readonly */ private MavenProject project; /** * The Maven Project Helper Object. * * @component * @required */ private MavenProjectHelper projectHelper; /** * Project classpath. * * @parameter default-value="${project.compileClasspathElements}" * @required * @readonly */ private List<String> classpathElements; /** * The directory for compiled classes. * * @parameter default-value="${project.build.outputDirectory}" * @required * @readonly */ private File outputDirectory; /** * The string to use for a user's home directory in the generated script. * @parameter default-value="$HOME" * @required */ private String homeExpression; /** * Deletes file-sets in the following project build directory order: (source) * directory, output directory, test directory, report directory, and then the * additional file-sets. * * @throws MojoExecutionException * When a directory failed to get deleted. * @see org.apache.maven.plugin.Mojo#execute() */ public void execute() throws MojoExecutionException { Scripter scripter = Guice.createInjector(new CodeGeneratorModule()).getInstance(Scripter.class); Path buildDirectory = getBuildDirectory().toPath(); Path classesDirectory = getOutputDirectory().toPath(); MavenProject project = getProject(); String version = project.getVersion(); Path targetPath = buildDirectory.resolve(project.getArtifactId() + "-" + version + ".sh"); Path workDirectory = buildDirectory.resolve("executable"); Iterable<Path> dependencyJarFiles = Iterables.transform(getClasspathElements(), new PathFunction()); try { scripter.generate(targetPath, classesDirectory, getHomeExpression(), version, workDirectory, dependencyJarFiles); //getProjectHelper(). attachArtifact(project, "sh", targetPath.toFile()); project.getArtifact().setFile(targetPath.toFile()); } catch (IOException | ExecutableScanException e) { throw new MojoExecutionException("Building an executable script failed.", e); } } /** * The Class PathFunction. */ static class PathFunction implements Function<String, Path> { /** * {@inheritDoc} */ @Override public Path apply(String path) { return Paths.get(path); } } /** * Gets the Maven project. * * @return the Maven project */ public MavenProject getProject() { return project; } /** * Gets the Maven Project Helper Object. * * @return the Maven Project Helper Object */ public MavenProjectHelper getProjectHelper() { return projectHelper; } /** * Gets the this is where build results go. * * @return the this is where build results go */ public File getBuildDirectory() { return buildDirectory; } /** * Gets the project classpath. * * @return the project classpath */ public List<String> getClasspathElements() { return classpathElements; } /** * Gets the directory for compiled classes. * * @return the directory for compiled classes */ public File getOutputDirectory() { return outputDirectory; } /** * Gets the string to use for a user's home directory in the generated script. * * @return the string to use for a user's home directory in the generated * script */ public String getHomeExpression() { return homeExpression; } /** * Sets the string to use for a user's home directory in the generated script. * * @param homeExpression * the new string to use for a user's home directory in the generated * script */ public void setHomeExpression(String homeExpression) { this.homeExpression = homeExpression; } }