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 final class ModuleDirectoryRemover implements Closeable { 36 /** 37 * The output directory expected by Maven 3. 38 * Example: {@code target/classes/META-INF/versions/21}. 39 */ 40 private final Path mavenTarget; 41 42 /** 43 * The output directory where {@code javac} will write the classes. 44 * Example: {@code target/classes/META-INF/versions/21/org.foo.bar}. 45 */ 46 private final Path javacTarget; 47 48 /** 49 * A temporary directory used as an intermediate step for avoiding name clash. 50 * Example: {@code target/classes/META-INF/versions/org.foo.bar}. 51 */ 52 private final Path interTarget; 53 54 /** 55 * Temporarily renames the given output directory for matching the layout of {@code javac} output. 56 * 57 * @param outputDirectory the output directory (must exist) 58 * @param moduleName the name of the module 59 * @throws IOException if an error occurred while renaming the output directory 60 */ 61 private ModuleDirectoryRemover(Path outputDirectory, String moduleName) throws IOException { 62 mavenTarget = outputDirectory; 63 interTarget = Files.move(outputDirectory, outputDirectory.resolveSibling(moduleName)); 64 javacTarget = Files.createDirectory(outputDirectory).resolve(moduleName); 65 Files.move(interTarget, javacTarget); 66 } 67 68 /** 69 * Temporarily renames the given output directory for matching the layout of {@code javac} output. 70 * 71 * @param outputDirectory the output directory (must exist) 72 * @param moduleName the name of the module, or {@code null} if none 73 * @return a handler for restoring the directory to its original name, or {@code null} if there is no renaming 74 * @throws IOException if an error occurred while renaming the output directory 75 */ 76 static ModuleDirectoryRemover create(Path outputDirectory, String moduleName) throws IOException { 77 return (moduleName != null) ? new ModuleDirectoryRemover(outputDirectory, moduleName) : null; 78 } 79 80 /** 81 * Restores the output directory to its original name. 82 * Note: contrarily to {@link Closeable} contract, this method is not idempotent: 83 * it cannot be executed twice. However, this is okay for the usage in this package. 84 * 85 * @throws IOException if an error occurred while renaming the output directory 86 */ 87 @Override 88 public void close() throws IOException { 89 Files.move(javacTarget, interTarget); 90 Files.delete(mavenTarget); 91 Files.move(interTarget, mavenTarget); 92 } 93 }