Java tutorial
/* * Copyright 2005-2006 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. */ package org.jfrog.jade.plugins.javac; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Javac; import org.apache.tools.ant.types.Path; import org.jfrog.jade.plugins.common.ant.Maven2AntManager; import org.jfrog.jade.plugins.common.injectable.MvnInjectableMojoSupport; import org.jfrog.maven.annomojo.annotations.MojoGoal; import org.jfrog.maven.annomojo.annotations.MojoParameter; import org.jfrog.maven.annomojo.annotations.MojoPhase; import org.jfrog.maven.annomojo.annotations.MojoRequiresDependencyResolution; import java.io.File; import java.util.List; /** * Sets the compile path to include additional sources * And uses Javac Ant task instead of plexus utils compiler * * @author Oren Sadeh */ @MojoGoal("compile") @MojoPhase("compile") @MojoRequiresDependencyResolution("compile") public class JavacCompilerMojo extends MvnInjectableMojoSupport { @MojoParameter(description = "Package being compiled. Only classes with package name starting with packageName will be compiled") private String packageName; @MojoParameter(description = "Additional source paths. Used at the beginning to solve circular dependencies") // TODO: more info? private List<String> sourceDependencies; @MojoParameter(required = true, defaultValue = "${project.compileClasspathElements}", readonly = true, description = "List of compile classpath elements.") private List<String> compileClasspathElements; @MojoParameter(required = true, defaultValue = "${project.compileSourceRoots}", readonly = true, description = "List of compile classpath elements.") private List<String> compileSourceRoots; @MojoParameter(description = "A list of inclusion filters for the compiler.") // TODO: more info? private List<String> includes; @MojoParameter(description = "A list of exclusion filters for the compiler.") private List<String> excludes; @MojoParameter(required = true, defaultValue = "${project.build.outputDirectory}", readonly = true, description = "The default output directory-o") private String outputDirectory; // ---------------------------------------------------------------------- // Configurables // Copied from AbstractCompilerMojo // (http://svn.apache.org/viewvc/maven/plugins/trunk/maven-compiler-plugin/src/main/java/org/apache/maven/plugin/AbstractCompilerMojo.java) // ---------------------------------------------------------------------- @MojoParameter(expression = "${maven.compiler.debug}", defaultValue = "true", description = "Whether to include debugging information in the compiled class files. The default value is true.") private boolean debug; @MojoParameter(expression = "${maven.compiler.showDeprecation}", defaultValue = "false", description = "Output source locations where deprecated APIs are used") private boolean showDeprecation; @MojoParameter(expression = "${maven.compiler.fork}", defaultValue = "true", description = "Fork the compiler if true") private boolean fork; @MojoParameter(expression = "${maven.compiler.optimize}", defaultValue = "false", description = "Optimize compiled code using the compiler's optimization methods") private boolean optimize; @MojoParameter(expression = "${maven.compiler.source}", description = "The -source argument for the Java compiler") private String source; @MojoParameter(expression = "${maven.compiler.executable}", description = "The javac executable to use when fork is true") private String executable; @MojoParameter(expression = "${maven.compiler.target}", description = " The -target argument for the Java compiler") private String target; public void execute() throws MojoExecutionException, MojoFailureException { Maven2AntManager mvnHelper = getMaven2AntManager(); Project antProject = getAntProject(); MavenProject project = getProject(); Javac javac = new Javac(); javac.setProject(antProject); Path src = javac.createSrc(); mvnHelper.fillPathFromPaths(src, compileSourceRoots); Path sourcePath = javac.createSourcepath(); mvnHelper.fillPathFromPaths(sourcePath, compileSourceRoots); if (sourceDependencies != null) { getLog().warn(""); getLog().warn("***********************************************"); getLog().warn("This module uses a non-standard compiler plugin"); getLog().warn("which compiles external code for circular"); getLog().warn("dependency workaround. The sourcesDependencies"); getLog().warn("configuration should be removed as soon as possible"); getLog().warn("***********************************************"); getLog().warn(""); mvnHelper.fillPathFromPaths(sourcePath, sourceDependencies); } if (packageName == null) { packageName = ""; } getLog().debug("Package name: " + packageName); javac.createPatternSet().createInclude().setName(packageName.replace('.', '/') + "/**/*.java"); if (includes != null) { for (String include : includes) { getLog().warn("Adding include pattern: " + include); javac.createPatternSet().createInclude().setName(include); } } if (excludes != null) { for (String exclude : excludes) { getLog().debug("Adding exclude pattern: " + exclude); javac.createPatternSet().createExclude().setName(exclude); } } mvnHelper.fillPathFromPaths(javac.createClasspath(), compileClasspathElements); File outputFileDir = new File(outputDirectory); if (!outputFileDir.exists()) { outputFileDir.mkdirs(); } javac.setDestdir(outputFileDir); javac.setSource(source); javac.setTarget(target); javac.setOptimize(optimize); javac.setDebug(debug); javac.setDeprecation(showDeprecation); javac.setFork(fork); javac.setExecutable(executable); javac.setFailonerror(true); try { javac.execute(); } catch (BuildException be) { throw new MojoExecutionException(be.getMessage(), be); } project.getArtifact().setFile(outputFileDir); } }