001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.maven; 018 019import java.io.File; 020import java.util.Map; 021 022import org.apache.maven.plugin.AbstractMojo; 023import org.apache.maven.plugin.MojoExecutionException; 024import org.apache.maven.plugin.MojoFailureException; 025import org.apache.maven.plugins.annotations.Component; 026import org.apache.maven.plugins.annotations.LifecyclePhase; 027import org.apache.maven.plugins.annotations.Mojo; 028import org.apache.maven.plugins.annotations.Parameter; 029import org.apache.maven.project.MavenProject; 030import org.sonatype.plexus.build.incremental.BuildContext; 031 032import org.springframework.boot.loader.tools.BuildPropertiesWriter; 033import org.springframework.boot.loader.tools.BuildPropertiesWriter.NullAdditionalPropertyValueException; 034import org.springframework.boot.loader.tools.BuildPropertiesWriter.ProjectDetails; 035 036/** 037 * Generate a {@code build-info.properties} file based the content of the current 038 * {@link MavenProject}. 039 * 040 * @author Stephane Nicoll 041 * @since 1.4.0 042 */ 043@Mojo(name = "build-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true) 044public class BuildInfoMojo extends AbstractMojo { 045 046 @Component 047 private BuildContext buildContext; 048 049 /** 050 * The Maven project. 051 */ 052 @Parameter(defaultValue = "${project}", readonly = true, required = true) 053 private MavenProject project; 054 055 /** 056 * The location of the generated build-info.properties. 057 */ 058 @Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties") 059 private File outputFile; 060 061 /** 062 * Additional properties to store in the build-info.properties. Each entry is prefixed 063 * by {@code build.} in the generated build-info.properties. 064 */ 065 @Parameter 066 private Map<String, String> additionalProperties; 067 068 @Override 069 public void execute() throws MojoExecutionException, MojoFailureException { 070 try { 071 new BuildPropertiesWriter(this.outputFile) 072 .writeBuildProperties(new ProjectDetails(this.project.getGroupId(), 073 this.project.getArtifactId(), this.project.getVersion(), 074 this.project.getName(), this.additionalProperties)); 075 this.buildContext.refresh(this.outputFile); 076 } 077 catch (NullAdditionalPropertyValueException ex) { 078 throw new MojoFailureException( 079 "Failed to generate build-info.properties. " + ex.getMessage(), ex); 080 } 081 catch (Exception ex) { 082 throw new MojoExecutionException(ex.getMessage(), ex); 083 } 084 } 085 086}