View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.compiler;
20  
21  import java.io.Closeable;
22  import java.io.IOException;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  
26  /**
27   * Helper class for removing the directory having a module name.
28   * This hack is used when the {@code --module-source-path} compiler option is used
29   * but we still want to reproduce the directory layout of Maven 3.
30   * This hack is not used when the new {@code <source>} elements is used instead.
31   *
32   * <p>The code in this class is useful only when {@link AbstractCompilerMojo#SUPPORT_LEGACY} is true.
33   * This class can be fully deleted if a future version permanently set above-cited flag to false.</p>
34   *
35   * @see CompilerMojo#directoryLevelToRemove
36   * @see ToolExecutorForTest#directoryLevelToRemove
37   */
38  final class ModuleDirectoryRemover implements Closeable {
39      /**
40       * The output directory expected by Maven 3.
41       * Example: {@code target/classes/META-INF/versions/21}.
42       */
43      private final Path mavenTarget;
44  
45      /**
46       * The output directory where {@code javac} will write the classes.
47       * Example: {@code target/classes/META-INF/versions/21/org.foo.bar}.
48       */
49      private final Path javacTarget;
50  
51      /**
52       * A temporary directory used as an intermediate step for avoiding name clash.
53       * Example: {@code target/classes/META-INF/versions/org.foo.bar}.
54       */
55      private final Path interTarget;
56  
57      /**
58       * Temporarily renames the given output directory for matching the layout of {@code javac} output.
59       *
60       * @param  outputDirectory  the output directory (must exist)
61       * @param  moduleName       the name of the module
62       * @throws IOException if an error occurred while renaming the output directory
63       */
64      private ModuleDirectoryRemover(Path outputDirectory, String moduleName) throws IOException {
65          mavenTarget = outputDirectory;
66          interTarget = Files.move(outputDirectory, outputDirectory.resolveSibling(moduleName));
67          javacTarget = Files.createDirectory(outputDirectory).resolve(moduleName);
68          Files.move(interTarget, javacTarget);
69      }
70  
71      /**
72       * Temporarily renames the given output directory for matching the layout of {@code javac} output.
73       *
74       * @param  outputDirectory  the output directory (must exist)
75       * @param  moduleName       the name of the module, or {@code null} if none
76       * @return a handler for restoring the directory to its original name, or {@code null} if there is no renaming
77       * @throws IOException if an error occurred while renaming the output directory
78       */
79      static ModuleDirectoryRemover create(Path outputDirectory, String moduleName) throws IOException {
80          return (moduleName != null) ? new ModuleDirectoryRemover(outputDirectory, moduleName) : null;
81      }
82  
83      /**
84       * Restores the output directory to its original name.
85       * Note: contrarily to {@link Closeable} contract, this method is not idempotent:
86       * it cannot be executed twice. However, this is okay for the usage in this package.
87       *
88       * @throws IOException if an error occurred while renaming the output directory
89       */
90      @Override
91      public void close() throws IOException {
92          Files.move(javacTarget, interTarget);
93          Files.delete(mavenTarget);
94          Files.move(interTarget, mavenTarget);
95      }
96  }