blob: 8788f39e07ab223bb5e2c3a909948bef59fce538 [file] [log] [blame]
package org.apache.maven.plugin.coreit;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
/**
* @goal touch
*
* @phase process-sources
*/
public class TouchMojo
extends AbstractMojo
{
static final String FINAL_NAME = "coreitified";
/**
* @parameter default-value="${project}"
*/
private MavenProject project;
/**
* @parameter default-value="${project.build.directory}"
* @required
*/
private File outputDirectory;
public void execute()
throws MojoExecutionException
{
getLog().info( "[MAVEN-CORE-IT-LOG] Project build directory " + project.getBuild().getDirectory() );
touch( new File( project.getBuild().getDirectory() ), "touch.log" );
getLog().info( "[MAVEN-CORE-IT-LOG] Using output directory " + outputDirectory );
touch( outputDirectory, "touch.txt" );
project.getBuild().setFinalName( FINAL_NAME );
}
static void touch( File dir, String file )
throws MojoExecutionException
{
try
{
if ( !dir.exists() )
{
dir.mkdirs();
}
File touch = new File( dir, file );
if ( !Files.exists( touch.toPath() ) )
{
Files.createFile( touch.toPath() );
}
// NOTE: Using append mode to track execution count
try ( BufferedWriter w = Files.newBufferedWriter( touch.toPath(), Charset.forName( "UTF-8" ),
StandardOpenOption.APPEND ) )
{
w.write( file );
w.write( "\n" );
}
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error touching file", e );
}
}
}