1 package org.apache.maven.plugin.changes; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import java.io.File; 23 import java.text.ParseException; 24 import java.text.SimpleDateFormat; 25 import java.util.List; 26 27 import org.apache.commons.lang.StringUtils; 28 import org.apache.maven.plugin.AbstractMojo; 29 import org.apache.maven.plugin.MojoExecutionException; 30 import org.apache.maven.plugins.changes.model.Release; 31 32 /** 33 * Goal which checks that the changes.xml file has the necessary data to 34 * generate an announcement or a report for the current release. 35 * 36 * @goal changes-check 37 * @author Justin Edelson 38 * @author Dennis Lundberg 39 * @since 2.4 40 * @threadSafe 41 */ 42 public class ChangesCheckMojo extends AbstractMojo 43 { 44 /** 45 * The format that a correct release date should have. This value will be 46 * used as a pattern to try to create a date. 47 * 48 * @parameter expression="${changes.releaseDateFormat}" default-value="yyyy-MM-dd" 49 */ 50 private String releaseDateFormat; 51 52 /** 53 * Version of the artifact. 54 * 55 * @parameter expression="${changes.version}" default-value="${project.version}" 56 * @required 57 */ 58 private String version; 59 60 /** 61 * The path of the <code>changes.xml</code> file that will be checked. 62 * 63 * @parameter expression="${changes.xmlPath}" default-value="src/changes/changes.xml" 64 */ 65 private File xmlPath; 66 67 /** 68 * Flag controlling snapshot processing. If set, versions ending with <code>-SNAPSHOT</code> won't be checked. 69 * 70 * @parameter expression="${changes.skipSnapshots}" default-value="false" 71 * @since 2.7 72 */ 73 private boolean skipSnapshots; 74 75 private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() ); 76 77 /** 78 * Check that the latest release contains a valid release date. 79 * 80 * @throws MojoExecutionException 81 */ 82 public void execute() 83 throws MojoExecutionException 84 { 85 if ( this.version.endsWith( "-SNAPSHOT" ) && this.skipSnapshots ) 86 { 87 getLog().info( "Skipping snapshot version '" + this.version + "'." ); 88 } 89 else if ( xmlPath.exists() ) 90 { 91 ChangesXML xml = new ChangesXML( xmlPath, getLog() ); 92 ReleaseUtils releaseUtils = new ReleaseUtils( getLog() ); 93 Release release = 94 releaseUtils.getLatestRelease( releaseUtils.convertReleaseList( xml.getReleaseList() ), version ); 95 96 if ( !isValidDate( release.getDateRelease(), releaseDateFormat ) ) 97 { 98 throw new MojoExecutionException( 99 "The file " + xmlPath.getAbsolutePath() + " has an invalid release date." ); 100 101 } 102 } 103 else 104 { 105 getLog().warn( "The file " + xmlPath.getAbsolutePath() + " does not exist." ); 106 } 107 } 108 109 /** 110 * Use the pattern to try to parse a Date from the given string. 111 * 112 * @param string A date as text 113 * @param pattern A pattern that can be used by {@link SimpleDateFormat} 114 * @return <code>true</code> if the string can be parsed as a date using the pattern, otherwise <code>false</code> 115 */ 116 protected static boolean isValidDate( String string, String pattern ) 117 { 118 if ( StringUtils.isEmpty( string ) ) 119 { 120 return false; 121 } 122 123 if ( StringUtils.isEmpty( pattern ) ) 124 { 125 return false; 126 } 127 128 try 129 { 130 SimpleDateFormat df = new SimpleDateFormat( pattern ); 131 df.parse( string ); 132 return true; 133 } 134 catch ( ParseException e ) 135 { 136 return false; 137 } 138 } 139 }