/*
 * Copyright 2012-2013 the original author or authors.
 *
 * 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.springframework.boot.gradle.task;

import java.io.File;
import java.io.IOException;

import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.bundling.Jar;
import org.springframework.boot.gradle.SpringBootPluginExtension;
import org.springframework.boot.loader.tools.Repackager;

/**
 * Repackage task.
 * 
 * @author Phillip Webb
 */
public class Repackage extends DefaultTask {

	@TaskAction
	public void repackage() {
		Project project = getProject();
		final SpringBootPluginExtension extension = project.getExtensions().getByType(
				SpringBootPluginExtension.class);
		final ProjectLibraries libraries = new ProjectLibraries(project);
		if (extension.getProvidedConfiguration() != null) {
			libraries.setProvidedConfigurationName(extension.getProvidedConfiguration());
		}
		project.getTasks().withType(Jar.class, new Action<Jar>() {
			@Override
			public void execute(Jar archive) {
				File file = archive.getArchivePath();
				if (file.exists()) {
					Repackager repackager = new Repackager(file);
					repackager.setMainClass(extension.getMainClass());
					if (extension.convertLayout() != null) {
						repackager.setLayout(extension.convertLayout());
					}
					repackager.setBackupSource(extension.isBackupSource());
					try {
						repackager.repackage(libraries);
					} catch (IOException ex) {
						throw new IllegalStateException(ex.getMessage(), ex);
					}
				}
			}
		});
	}
}

@@ -1623,24 +1623,71 @@
  archive) %7B%0A
+%09%09%09%09if (%22%22.equals(archive.getClassifier())) %7B%0A%09
 %09%09%09%09File fil
@@ -1716,16 +1716,17 @@
 Path();%0A
+%09
 %09%09%09%09if (
@@ -1747,16 +1747,17 @@
  %7B%0A%09%09%09%09%09
+%09
 Repackag
@@ -1794,16 +1794,17 @@
 (file);%0A
+%09
 %09%09%09%09%09rep
@@ -1852,24 +1852,25 @@
 ss());%0A%09%09%09%09%09
+%09
 if (extensio
@@ -1894,24 +1894,25 @@
  != null) %7B%0A
+%09
 %09%09%09%09%09%09repack
@@ -1955,26 +1955,28 @@
 ut());%0A%09%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09%09repacka
@@ -2033,14 +2033,16 @@
 %09%09%09%09
+%09
 try %7B%0A
+%09
 %09%09%09%09
@@ -2081,16 +2081,17 @@
 );%0A%09%09%09%09%09
+%09
 %7D catch 
@@ -2105,24 +2105,25 @@
 ption ex) %7B%0A
+%09
 %09%09%09%09%09%09throw 
@@ -2170,16 +2170,24 @@
 ), ex);%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%7D%0A%09

Update Gradle plugin to only repackage main jar