Skip to content

Commit

Permalink
Remove most of commons.io usages
Browse files Browse the repository at this point in the history
- java.nio provides good enough API's to to basic file operations
- our code mostly uses java.nio now which makes it more simple
- less dependencies to 3rd parties  which needs to be updated
- FileAlternateObserver still left
  • Loading branch information
alexsuter committed Jul 29, 2024
1 parent 27ce6c7 commit 4a38990
Show file tree
Hide file tree
Showing 23 changed files with 318 additions and 127 deletions.
9 changes: 4 additions & 5 deletions src/main/java/ch/ivyteam/ivy/maven/InstallEngineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.maven.artifact.versioning.ArtifactVersion;
Expand All @@ -42,6 +41,7 @@
import ch.ivyteam.ivy.maven.engine.download.EngineDownloader;
import ch.ivyteam.ivy.maven.engine.download.MavenEngineDownloader;
import ch.ivyteam.ivy.maven.engine.download.URLEngineDownloader;
import ch.ivyteam.ivy.maven.util.PathUtils;
import net.lingala.zip4j.ZipFile;

/**
Expand Down Expand Up @@ -263,11 +263,10 @@ private void removeOldEngineContent() throws MojoExecutionException {
var dir = getRawEngineDirectory();
try {
if (dir != null) {
FileUtils.cleanDirectory(dir.toFile());
PathUtils.clean(dir);
}
} catch (IOException ex) {
throw new MojoExecutionException(
"Failed to clean outdated ivy Engine directory '" + dir + "'.", ex);
} catch (Exception ex) {
throw new MojoExecutionException("Failed to clean outdated ivy Engine directory '" + dir + "'.", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@

package ch.ivyteam.ivy.maven;

import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import ch.ivyteam.ivy.maven.util.PathUtils;

/**
* Delete copied maven dependencies in the lib/mvn-deps folder.
*
*
* @since 9.2.0
*/
@Mojo(name = MavenDependencyCleanupMojo.GOAL)
public class MavenDependencyCleanupMojo extends AbstractMojo {

public static final String GOAL = "maven-dependency-cleanup";

@Parameter(property = "project", required = true, readonly = true)
Expand All @@ -51,12 +51,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}
var mvnLibDir = project.getBasedir().toPath().resolve("lib").resolve("mvn-deps");
getLog().info("Deleting " + mvnLibDir.toString());
getLog().info("Deleting " + mvnLibDir);
try {
FileUtils.deleteDirectory(mvnLibDir.toFile());
} catch (IOException ex) {
getLog().warn("Couldn't delete: " + mvnLibDir.toString(), ex);
PathUtils.delete(mvnLibDir);
} catch (Exception ex) {
getLog().warn("Couldn't delete: " + mvnLibDir, ex);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ public class ShareEngineCoreClasspathMojo extends AbstractEngineMojo {

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
List<File> ivyEngineClassPathFiles = EngineClassLoaderFactory
.getIvyEngineClassPathFiles(identifyAndGetEngineDirectory().toFile());
List<File> ivyEngineClassPathFiles = EngineClassLoaderFactory.getIvyEngineClassPathFiles(identifyAndGetEngineDirectory());
String propertyValue = StringUtils.join(ivyEngineClassPathFiles, ",");

MavenProperties properties = new MavenProperties(project, getLog());
properties.setMavenProperty(IVY_ENGINE_CORE_CLASSPATH_PROPERTY, propertyValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected MavenProjectBuilderProxy getMavenProjectBuilder() throws Exception {
getLog(),
timeoutEngineStartInSeconds);
}
classLoaderFactory.writeEngineClasspathJar(toFile(engineDir));
classLoaderFactory.writeEngineClasspathJar(engineDir);
// share engine directory as property for custom follow up plugins:
if (engineDir != null) {
project.getProperties().put(AbstractEngineMojo.ENGINE_DIRECTORY_PROPERTY, engineDir.toAbsolutePath().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

package ch.ivyteam.ivy.maven.deploy;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.ReadOnlyFileSystemException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down Expand Up @@ -244,12 +243,14 @@ protected final Path createDeployOptionsFile(DeploymentOptionsFileFactory option
return null;
}

protected static final void removeTemporaryDeploymentOptionsFile(Path deploymentOptionsFile) {
FileUtils.deleteQuietly(toFile(deploymentOptionsFile));
}

private static File toFile(Path file) {
return file == null ? null : file.toFile();
protected static void deleteFile(Path file) {
if (file != null && Files.exists(file)) {
try {
Files.delete(file);
} catch (IOException ex) {
throw new UncheckedIOException("Could not delete " + file, ex);
}
}
}

protected final void deployToDirectory(Path resolvedOptionsFile, Path deployDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
try {
deployWithOptions(resolvedOptionsFile);
} finally {
removeTemporaryDeploymentOptionsFile(resolvedOptionsFile);
deleteFile(resolvedOptionsFile);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private void deployTestApp() throws MojoExecutionException {
var deployDir = getEngineDir(project).resolve(DeployToEngineMojo.DEPLOY_DEFAULT);
deployToDirectory(resolvedOptionsFile, deployDir);
} finally {
removeTemporaryDeploymentOptionsFile(resolvedOptionsFile);
deleteFile(resolvedOptionsFile);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.logging.Log;
Expand Down Expand Up @@ -72,12 +71,10 @@ public EngineClassLoaderFactory(MavenContext mavenContext) {
this.maven = mavenContext;
}

public URLClassLoader createEngineClassLoader(File engineDirectory) throws IOException {
public URLClassLoader createEngineClassLoader(Path engineDirectory) throws IOException {
List<File> osgiClasspath = getOsgiBootstrapClasspath(engineDirectory);
var filter = WildcardFileFilter.builder()
.setWildcards("org.eclipse.osgi_*.jar")
.get();
addToClassPath(osgiClasspath, new File(engineDirectory, OsgiDir.PLUGINS), filter);
var pluginsDir = engineDirectory.resolve(OsgiDir.PLUGINS);
addToClassPath(osgiClasspath, pluginsDir, p -> p.getFileName().toString().startsWith("org.eclipse.osgi_") && p.getFileName().toString().endsWith(".jar"));
osgiClasspath.addAll(0, getSlf4jJars());
if (maven.log.isDebugEnabled()) {
maven.log.debug("Configuring OSGi engine classpath:");
Expand All @@ -93,35 +90,55 @@ public List<File> getSlf4jJars() {
maven.getJar("org.slf4j", "log4j-over-slf4j", SLF4J_VERSION));
}

public static List<File> getOsgiBootstrapClasspath(File engineDirectory) {
if (engineDirectory == null || !engineDirectory.isDirectory()) {
public static List<File> getOsgiBootstrapClasspath(Path engineDirectory) {
if (engineDirectory == null || !Files.isDirectory(engineDirectory)) {
throw new RuntimeException("The engineDirectory is missing: " + engineDirectory);
}
List<File> classPathFiles = new ArrayList<>();
addToClassPath(classPathFiles, new File(engineDirectory, OsgiDir.INSTALL_AREA + "/" + OsgiDir.LIB_BOOT),
new SuffixFileFilter(".jar"));
var libBoot = engineDirectory.resolve(OsgiDir.INSTALL_AREA).resolve(OsgiDir.LIB_BOOT);
addToClassPath(classPathFiles, libBoot, p -> p.getFileName().toString().endsWith(".jar"));
return classPathFiles;
}

private static void addToClassPath(List<File> classPathFiles, File dir, IOFileFilter fileFilter) {
if (dir.isDirectory()) {
classPathFiles.addAll(FileUtils.listFiles(dir, fileFilter, null));
private static void addToClassPath(List<File> classPathFiles, Path dir, Predicate<Path> filter) {
if (Files.isDirectory(dir)) {
try (var stream = Files.list(dir)) {
var files = stream
.filter(filter)
.map(p -> p.toFile())
.toList();
classPathFiles.addAll(files);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}

public static List<File> getIvyEngineClassPathFiles(File engineDirectory) {
List<File> classPathFiles = new ArrayList<>();
public static List<File> getIvyEngineClassPathFiles(Path engineDirectory) {
if (engineDirectory == null) {
return List.of();
}

var classPathFiles = new ArrayList<File>();
for (String libDirPath : ENGINE_LIB_DIRECTORIES) {
File jarDir = new File(engineDirectory, libDirPath + File.separator);
if (!jarDir.isDirectory()) {
var jarDir = engineDirectory.resolve(libDirPath);
if (!Files.isDirectory(jarDir)) {
continue;
}
classPathFiles.addAll(FileUtils.listFiles(jarDir, new String[] {"jar"}, true));
try (var walker = Files.walk(jarDir)) {
var jars = walker
.filter(p -> p.getFileName().toString().endsWith(".jar"))
.map(p -> p.toFile())
.toList();
classPathFiles.addAll(jars);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
return classPathFiles;
}

public void writeEngineClasspathJar(File engineDirectory) throws IOException {
public void writeEngineClasspathJar(Path engineDirectory) throws IOException {
writeEngineClasspathJar(getIvyEngineClassPathFiles(engineDirectory));
}

Expand Down Expand Up @@ -163,5 +180,4 @@ public File getJar(String groupId, String artifactId, String version) {
return jar;
}
}

}
}
10 changes: 8 additions & 2 deletions src/main/java/ch/ivyteam/ivy/maven/engine/EngineControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.exec.ShutdownHookProcessDestroyer;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;

Expand Down Expand Up @@ -272,8 +271,15 @@ private String executeSynch(CommandLine statusCmd) {
executor.execute(statusCmd);
} catch (IOException ex) { // expected!
} finally {
try {
engineOutput = outputStream.toString();
IOUtils.closeQuietly(outputStream);
} finally {
try {
outputStream.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
return engineOutput;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private Path setupEngineClasspathJarIfNotExists() {
try {
log.info("Creating a classpath jar for starting the engine");
new ClasspathJar(classpathJar.toFile())
.createFileEntries(EngineClassLoaderFactory.getOsgiBootstrapClasspath(engineDirectory.toFile()));
.createFileEntries(EngineClassLoaderFactory.getOsgiBootstrapClasspath(engineDirectory));
} catch (Exception ex) {
throw new RuntimeException(
"Could not create engine classpath jar: '" + classpathJar + "'", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public MavenProjectBuilderProxy(EngineClassLoaderFactory classLoaderFactory, Fil

logEngine();

URLClassLoader ivyEngineClassLoader = classLoaderFactory.createEngineClassLoader(baseDirToBuildIn);
URLClassLoader ivyEngineClassLoader = classLoaderFactory.createEngineClassLoader(baseDirToBuildIn.toPath());
delegateClass = getOsgiBundledDelegate(ivyEngineClassLoader, timeoutEngineStartInSeconds);
Constructor<?> constructor = delegateClass.getDeclaredConstructor(File.class);

delegate = executeInEngineDir(() -> constructor.newInstance(workspace));

List<File> engineJars = EngineClassLoaderFactory.getIvyEngineClassPathFiles(baseDirToBuildIn);
List<File> engineJars = EngineClassLoaderFactory.getIvyEngineClassPathFiles(baseDirToBuildIn.toPath());
engineClasspath = getEngineClasspath(engineJars);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import java.nio.file.Path;
import java.util.Collections;

import org.apache.commons.io.FilenameUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.filtering.MavenFileFilter;
import org.apache.maven.shared.filtering.MavenFilteringException;

import ch.ivyteam.ivy.maven.util.PathUtils;

public class DeploymentOptionsFileFactory {

private static final String YAML = "yaml";
Expand All @@ -28,8 +29,8 @@ public Path createFromTemplate(Path optionsFile, MavenProject project, MavenSess
return null;
}

String fileFormat = FilenameUtils.getExtension(optionsFile.getFileName().toString());
var targetFile = getTargetFile(fileFormat);
var ext = PathUtils.toExtension(optionsFile);
var targetFile = getTargetFile(ext);
try {
fileFilter.copyFile(optionsFile.toFile(), targetFile.toFile(), true, project, Collections.emptyList(), false,
StandardCharsets.UTF_8.name(), session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
*/
package ch.ivyteam.ivy.maven.engine.deploy.dir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

Expand Down Expand Up @@ -71,21 +69,20 @@ private void clear() {
private void initDeployment() throws MojoExecutionException {
try {
if (deploymentOptionsFile != null) {
//var engineOption = deploymentFiles.getDeployCandidate().resolveSibling(deploymentOptionsFile.getName());
//Files.copy(deploymentOptionsFile.toPath(), engineOption);
File engineOption = new File(deploymentFiles.getDeployCandidate().getParent().toFile(), deploymentOptionsFile.getFileName().toString());
FileUtils.copyFile(deploymentOptionsFile.toFile(), engineOption);
var engineOption = deploymentFiles.getDeployCandidate().resolveSibling(deploymentOptionsFile.getFileName().toString());
Files.createDirectories(engineOption.getParent());
Files.copy(deploymentOptionsFile, engineOption);
}
} catch (IOException ex) {
throw new MojoExecutionException("Failed to initialize engine deployment, could not copy options file",
ex);
throw new MojoExecutionException("Failed to initialize engine deployment, could not copy options file", ex);
}
}

private void copyDeployableToEngine() throws MojoExecutionException {
try {
log.info("Uploading file " + deployFile + " to " + targetDeployableFile);
FileUtils.copyFile(deployFile.toFile(), targetDeployableFile.toFile());
Files.createDirectories(targetDeployableFile.getParent());
Files.copy(deployFile, targetDeployableFile);
} catch (IOException ex) {
throw new MojoExecutionException("Upload of file '" + deployFile.getFileName().toString() + "' to engine failed.", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @since 6.1.0
*/
class FileLogForwarder {

private final Path engineLog;
private final Log mavenLog;

Expand Down Expand Up @@ -102,5 +103,4 @@ private void readNewLines(RandomAccessFile readableLog) throws IOException {
static interface LogLineHandler {
void handleLine(String newLogLine);
}

}
}
Loading

0 comments on commit 4a38990

Please sign in to comment.