View Javadoc
1   package io.guixer.maven;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   
5   import java.io.File;
6   import java.io.IOException;
7   
8   import org.apache.maven.plugin.AbstractMojo;
9   import org.apache.maven.plugin.MojoExecutionException;
10  import org.apache.maven.project.MavenProject;
11  
12  import io.guixer.logs.GuixerOut;
13  import io.guixer.logs.GuixerOutLoader;
14  
15  public abstract class AbstractGuixerOutMojo extends AbstractMojo {
16  
17  	protected static File getTargetDir(
18  		final MavenProject project
19  	) throws MojoExecutionException {
20  
21  		final String targetDirPath = project.getBuild().getDirectory();
22  
23  		final File plainTargetDir = new File(targetDirPath);
24  
25  		if (plainTargetDir.isDirectory()) {
26  
27  			return plainTargetDir;
28  		}
29  
30  		final File stupidTargetDir = new File("target");
31  
32  		if (stupidTargetDir.isDirectory()) {
33  
34  			return stupidTargetDir;
35  		}
36  
37  		throw new MojoExecutionException("\"target\" (build) directory cannot be found");
38  	}
39  
40  	protected final File getGuixerOutDirectory(
41  		final MavenProject project
42  	) throws MojoExecutionException {
43  
44  		checkNotNull(project, "project");
45  
46  		final File targetDir = getTargetDir(project);
47  
48  		final File guixerOutDir = new File(targetDir, "guixer_out");
49  
50  		if (!guixerOutDir.exists()) {
51  
52  			throw new MojoExecutionException("Cannot find guixer_out directory: " + guixerOutDir.getAbsolutePath());
53  
54  		} else if (!guixerOutDir.isDirectory()) {
55  
56  			throw new MojoExecutionException(
57  					"guixer_out should be a directory, but was: " + guixerOutDir.getAbsolutePath());
58  		}
59  
60  		return guixerOutDir;
61  	}
62  
63  	protected final GuixerOut loadGuixerOut(
64  		final MavenProject project
65  	) throws MojoExecutionException {
66  
67  		checkNotNull(project, "project");
68  
69  		final File guixerOutDir = getGuixerOutDirectory(project);
70  
71  		final String guixerOutDirPath;
72  
73  		try {
74  
75  			guixerOutDirPath = guixerOutDir.getCanonicalPath();
76  
77  		} catch (final IOException e) {
78  
79  			throw new MojoExecutionException(
80  					"Unable to work with guixer_out directory: " + guixerOutDir.getAbsolutePath(), e);
81  		}
82  
83  		final GuixerOut guixerOut;
84  
85  		try {
86  
87  			guixerOut = new GuixerOutLoader(guixerOutDir).getGuixerOut();
88  
89  		} catch (final IOException e) {
90  
91  			throw new MojoExecutionException("Cannot read guixer_out directory: " + guixerOutDirPath);
92  		}
93  
94  		return guixerOut;
95  	}
96  }