Java tutorial
/* * Copyright 2012 Brian Matthews * * 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 com.btmatthews.maven.plugins.xcodebuilder; import java.io.File; import java.util.List; import java.util.Map; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.cli.CommandLineException; import org.codehaus.plexus.util.cli.CommandLineUtils; import org.codehaus.plexus.util.cli.Commandline; /** * Abstract base class that implements the * * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a> * @since 1.0.0 */ public abstract class AbstractProjectMojo extends AbstractMojo { @Parameter(property = "project") private File project; @Parameter(property = "targets") private List<String> targets; @Parameter(property = "arch") private String arch; @Parameter(property = "configuration") private String configuration; @Parameter(property = "sdk") private String sdk; @Parameter(property = "settings") private Map<String, String> settings; /** * Execute the command line build by the sub-class implementation of the {@link AbstractProjectMojo#buildCommand()} * method. Output from the command line execution is logged as an error if it was written to stderr or logged as * an information message if it was written to stdout. * * @throws MojoExecutionException If there was a problem executing the build command itself. * @throws MojoFailureException If the build command reported an error. */ public void execute() throws MojoExecutionException, MojoFailureException { final Commandline command = buildCommand(); try { final int rc = CommandLineUtils.executeCommandLine(command, new InfoConsumer(getLog()), new ErrorConsumer(getLog())); if (rc != 0) { throw new MojoExecutionException("Error while executing xcodebuild command. Return code was " + rc); } } catch (CommandLineException e) { throw new MojoExecutionException("Unable to execute xcodebuild command", e); } } /** * Build the command line that will be executed. * * @return The command line object. */ public Commandline buildCommand() { final Commandline command = new Commandline(); command.setExecutable("xcodebuild"); // Set the project if (project != null) { command.createArg().setValue("-project"); command.createArg().setFile(project); } // Set the targets boolean allTargets = true; if (targets != null) { for (final String target : targets) { if (StringUtils.isBlank(target)) { allTargets = false; command.createArg().setValue("-target"); command.createArg().setValue(target); } } } if (allTargets) { command.createArg().setValue("-alltargets"); } // Set the architecture (optional) if (StringUtils.isNotBlank(arch)) { command.createArg().setValue("-arch"); command.createArg().setValue(arch); } // Set the configuration (optional) if (StringUtils.isNotBlank(configuration)) { command.createArg().setValue("-configuration"); command.createArg().setValue(configuration); } // Set the SDK (optional) if (StringUtils.isNotBlank(sdk)) { command.createArg().setValue("-sdk"); command.createArg().setValue(sdk); } // Specify the build action command.createArg().setValue(getAction()); // Add any build settings if (settings != null) { for (Map.Entry<String, String> setting : settings.entrySet()) { final StringBuilder value = new StringBuilder(setting.getKey()); value.append('='); value.append(setting.getValue()); command.createArg().setValue(value.toString()); } } return command; } protected abstract String getAction(); }