/*
 * #%L
 * Native ARchive plugin for Maven
 * %%
 * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
 * %%
 * 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.
 * #L%
 */
package com.github.maven_nar;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
 * Generates a NarSystem class with static methods to use inside the java part
 * of the library. Runs in generate-resources rather than generate-sources to
 * allow the maven-swig-plugin (which runs in generate-sources) to configure the
 * nar plugin and to let it generate a proper system file.
 * 
 * @author Mark Donszelmann
 */
@Mojo(name = "nar-system-generate", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, requiresProject = true)
public class NarSystemMojo
    extends AbstractNarMojo
{

	/** @component */
	private BuildContext buildContext;

	@Override
	public final void narExecute() throws MojoExecutionException, MojoFailureException {
		// get packageName if specified for JNI.
		String packageName = null;
		String narSystemName = null;
		File narSystemDirectory = null;
		boolean jniFound = false;
		for (final Library library : getLibraries()) {
			if (library.getType().equals(Library.JNI) || library.getType().equals(Library.SHARED)) {
				packageName = library.getNarSystemPackage();
				narSystemName = library.getNarSystemName();
				narSystemDirectory = new File(getTargetDirectory(), library.getNarSystemDirectory());
				jniFound = true;
			}
		}

		if (!jniFound || packageName == null) {
			if (!jniFound) {
				getLog().debug("NAR: not building a shared or JNI library, so not generating NarSystem class.");
			} else {
				getLog().warn("NAR: no system package specified; unable to generate NarSystem class.");
			}
			return;
		}

		// make sure destination is there
		narSystemDirectory.mkdirs();

		getMavenProject().addCompileSourceRoot(narSystemDirectory.getPath());

		final File fullDir = new File(narSystemDirectory, packageName.replace('.', '/'));
		fullDir.mkdirs();

		final File narSystem = new File(fullDir, narSystemName + ".java");
		getLog().info("Generating " + narSystem);
		try {
			final String output = getOutput(true);
			final FileOutputStream fos = new FileOutputStream(narSystem);
			final PrintWriter p = new PrintWriter(fos);
			p.println("// DO NOT EDIT: Generated by NarSystemGenerate.");
			p.println("package " + packageName + ";");
			p.println("");
			p.println("/**");
			p.println(" * Generated class to load the correct version of the jni library");
			p.println(" *");
			p.println(" * @author nar-maven-plugin");
			p.println(" */");
			p.println("public final class NarSystem");
			p.println("{");
			p.println("");
			p.println("    private NarSystem() ");
			p.println("    {");
			p.println("    }");
			p.println("");
			p.println("   /**");
			p.println("    * Load jni library: " + output);
			p.println("    *");
			p.println("    * @author nar-maven-plugin");
			p.println("    */");
			p.println("    public static void loadLibrary()");
			p.println("    {");
			p.println("        System.loadLibrary(\"" + output + "\");");
			p.println("    }");
			p.println("");
			p.println("    public static int runUnitTests() {");
			p.println("	       return new NarSystem().runUnitTestsNative();");
			p.println("}");
			p.println("");
			p.println("    public native int runUnitTestsNative();");
			p.println("}");
			p.close();
			fos.close();
		} catch (final IOException e) {
			throw new MojoExecutionException("Could not write '" + narSystemName + "'", e);
		}

		if (buildContext != null) {
			buildContext.refresh(narSystem);
		}
	}
}

@@ -4104,32 +4104,36 @@
 ;%0A%09%09%09p.println(%22
+    
 %7D%22);%0A%09%09%09p.printl

Fix indentation of generated NarSystem class