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 private ReleaseUtils releaseUtils = new ReleaseUtils( getLog() ); 68 69 /** 70 * Check that the latest release contains a valid release date. 71 * 72 * @throws MojoExecutionException 73 */ 74 public void execute() 75 throws MojoExecutionException 76 { 77 if ( xmlPath.exists() ) 78 { 79 ChangesXML xml = new ChangesXML( xmlPath, getLog() ); 80 ReleaseUtils releaseUtils = new ReleaseUtils( getLog() ); 81 Release release = releaseUtils.getLatestRelease( releaseUtils.convertReleaseList( xml.getReleaseList() ), 82 version ); 83 if ( !isValidDate( release.getDateRelease(), releaseDateFormat ) ) 84 { 85 throw new MojoExecutionException( 86 "The file " + xmlPath.getAbsolutePath() + " has an invalid release date." ); 87 } 88 } 89 else 90 { 91 getLog().warn( "The file " + xmlPath.getAbsolutePath() + " does not exist." ); 92 } 93 } 94 95 /** 96 * Use the pattern to try to parse a Date from the given string. 97 * 98 * @param string A date as text 99 * @param pattern A pattern that can be used by {@link SimpleDateFormat} 100 * @return <code>true</code> if the string can be parsed as a date using the pattern, otherwise <code>false</code> 101 */ 102 protected static boolean isValidDate( String string, String pattern ) 103 { 104 if ( StringUtils.isEmpty( string ) ) 105 { 106 return false; 107 } 108 109 if ( StringUtils.isEmpty( pattern ) ) 110 { 111 return false; 112 } 113 114 try 115 { 116 SimpleDateFormat df = new SimpleDateFormat( pattern ); 117 df.parse( string ); 118 return true; 119 } 120 catch ( ParseException e ) 121 { 122 return false; 123 } 124 } 125 }