001/* 002 * Copyright 2012-2018 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.time.Instant; 021import java.util.Map; 022 023import org.apache.maven.plugin.AbstractMojo; 024import org.apache.maven.plugin.MojoExecutionException; 025import org.apache.maven.plugin.MojoFailureException; 026import org.apache.maven.plugins.annotations.Component; 027import org.apache.maven.plugins.annotations.LifecyclePhase; 028import org.apache.maven.plugins.annotations.Mojo; 029import org.apache.maven.plugins.annotations.Parameter; 030import org.apache.maven.project.MavenProject; 031import org.sonatype.plexus.build.incremental.BuildContext; 032 033import org.springframework.boot.loader.tools.BuildPropertiesWriter; 034import org.springframework.boot.loader.tools.BuildPropertiesWriter.NullAdditionalPropertyValueException; 035import org.springframework.boot.loader.tools.BuildPropertiesWriter.ProjectDetails; 036 037/** 038 * Generate a {@code build-info.properties} file based the content of the current 039 * {@link MavenProject}. 040 * 041 * @author Stephane Nicoll 042 * @since 1.4.0 043 */ 044@Mojo(name = "build-info", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true) 045public class BuildInfoMojo extends AbstractMojo { 046 047 @Component 048 private BuildContext buildContext; 049 050 /** 051 * The Maven project. 052 */ 053 @Parameter(defaultValue = "${project}", readonly = true, required = true) 054 private MavenProject project; 055 056 /** 057 * The location of the generated build-info.properties. 058 */ 059 @Parameter(defaultValue = "${project.build.outputDirectory}/META-INF/build-info.properties") 060 private File outputFile; 061 062 /** 063 * Additional properties to store in the build-info.properties. Each entry is prefixed 064 * by {@code build.} in the generated build-info.properties. 065 */ 066 @Parameter 067 private Map<String, String> additionalProperties; 068 069 @Override 070 public void execute() throws MojoExecutionException, MojoFailureException { 071 try { 072 new BuildPropertiesWriter(this.outputFile) 073 .writeBuildProperties(new ProjectDetails(this.project.getGroupId(), 074 this.project.getArtifactId(), this.project.getVersion(), 075 this.project.getName(), Instant.now(), 076 this.additionalProperties)); 077 this.buildContext.refresh(this.outputFile); 078 } 079 catch (NullAdditionalPropertyValueException ex) { 080 throw new MojoFailureException( 081 "Failed to generate build-info.properties. " + ex.getMessage(), ex); 082 } 083 catch (Exception ex) { 084 throw new MojoExecutionException(ex.getMessage(), ex); 085 } 086 } 087 088}