View Javadoc
1   /*
2    *    Copyright 2011-2024 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.commonjava.maven.plugins.execroot;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.project.MavenProject;
29  
30  /**
31   * The Class AbstractDirectoryGoal.
32   */
33  public abstract class AbstractDirectoryGoal extends AbstractMojo {
34  
35      /** The property. */
36      @Parameter(defaultValue = "dirProperty", required = true)
37      protected String property;
38  
39      /** The current project. */
40      @Parameter(defaultValue = "${project}", readonly = true)
41      protected MavenProject currentProject;
42  
43      /** The session. */
44      @Parameter(defaultValue = "${session}", readonly = true)
45      protected MavenSession session;
46  
47      /** The quiet. */
48      @Parameter(defaultValue = "false")
49      protected boolean quiet;
50  
51      /** The system property. */
52      @Parameter(defaultValue = "false")
53      protected boolean systemProperty;
54  
55      /** The lock obj. */
56      private final Object lockObj = new Object();
57  
58      /**
59       * Skip run of plugin.
60       *
61       * @since 1.1.0
62       */
63      @Parameter(defaultValue = "false", property = "directory.skip")
64      private boolean skip;
65  
66      @Override
67      public final void execute() throws MojoExecutionException, MojoFailureException {
68          // Check if plugin run should be skipped
69          if (this.skip) {
70              getLog().info("Directory Plugin is skipped");
71              return;
72          }
73  
74          File execRoot;
75          synchronized (lockObj) {
76              final String key = getContextKey();
77              execRoot = (File) getPluginContext().get(key);
78              if (execRoot == null) {
79                  execRoot = findDirectory();
80                  getPluginContext().put(key, execRoot);
81              }
82          }
83  
84          if (!quiet) {
85              getLog().info(getLogLabel() + " set to: " + execRoot);
86          }
87  
88          currentProject.getProperties().setProperty(property, execRoot.getAbsolutePath());
89  
90          if (systemProperty) {
91              String existingValue = System.getProperty(property);
92              if (existingValue == null) {
93                  System.setProperty(property, execRoot.getAbsolutePath());
94              }
95          }
96  
97          if (getLog().isDebugEnabled()) {
98              try (StringWriter str = new StringWriter();
99                      PrintWriter print = new PrintWriter(str)) {
100                 currentProject.getProperties().list(print);
101 
102                 getLog().debug("After setting property '" + property + "', project properties are:\n\n" + str);
103             } catch (IOException e) {
104                 throw new MojoExecutionException("Failed to write properties", e);
105             }
106         }
107     }
108 
109     /**
110      * Gets the log label.
111      *
112      * @return the log label
113      */
114     protected abstract String getLogLabel();
115 
116     /**
117      * Find directory.
118      *
119      * @return the file
120      *
121      * @throws MojoExecutionException
122      *             the mojo execution exception
123      */
124     protected abstract File findDirectory() throws MojoExecutionException;
125 
126     /**
127      * Gets the context key.
128      *
129      * @return the context key
130      */
131     protected abstract String getContextKey();
132 
133 }