From 14654f3055754ef617ff7ffec222aeebc08e7315 Mon Sep 17 00:00:00 2001 From: Alexander Suter Date: Mon, 22 Jul 2024 15:23:49 +0200 Subject: [PATCH] Convert from java.io to java.nio --- .../ivyteam/ivy/maven/AbstractEngineMojo.java | 2 +- .../ivyteam/ivy/maven/InstallEngineMojo.java | 19 +++--- .../ivy/maven/compile/CompileProjectMojo.java | 4 +- .../maven/compile/CompileTestProjectMojo.java | 4 +- .../ivy/maven/deploy/DeployToEngineMojo.java | 14 ++--- .../engine/EngineClassLoaderFactory.java | 2 +- .../ivy/maven/engine/EngineControl.java | 21 +++---- .../ivy/maven/engine/EngineModuleHints.java | 8 +-- .../ivy/maven/engine/EngineMojoContext.java | 16 ++--- .../maven/engine/EngineVersionEvaluator.java | 23 ++++---- .../engine/MavenProjectBuilderProxy.java | 4 +- .../ivyteam/ivy/maven/engine/OsgiRuntime.java | 13 ++-- .../deploy/DeploymentOptionsFileFactory.java | 1 - .../engine/deploy/dir/DeploymentFiles.java | 40 ++++++++----- .../maven/engine/deploy/dir/FileDeployer.java | 24 ++++---- .../engine/deploy/dir/FileLogForwarder.java | 9 +-- .../engine/download/EngineDownloader.java | 4 +- .../download/MavenEngineDownloader.java | 6 +- .../engine/download/URLEngineDownloader.java | 21 ++++--- .../test/AbstractIntegrationTestMojo.java | 7 ++- .../SetupIntegrationTestPropertiesMojo.java | 2 +- .../test/SetupIvyTestPropertiesMojo.java | 15 ++--- .../ivy/maven/test/StartTestEngineMojo.java | 23 ++++---- .../ivy/maven/test/StopTestEngineMojo.java | 8 +-- .../ivy/maven/test/bpm/IvyTestRuntime.java | 26 ++++---- .../ivy/maven/util/CompilerResult.java | 19 +++--- .../ch/ivyteam/ivy/maven/util/SharedFile.java | 26 ++++---- .../ivy/maven/BaseEngineProjectMojoTest.java | 2 +- .../ivy/maven/TestInstallEngineMojo.java | 6 +- .../maven/deploy/TestDeployToEngineMojo.java | 59 +++++++++---------- .../deploy/TestDeployToRunningEngine.java | 26 ++++---- .../engine/TestEngineVersionEvaluator.java | 4 +- .../deploy/dir/TestFileLogForwarder.java | 2 +- .../test/TestSetupIvyTestPropertiesMojo.java | 11 ++-- 34 files changed, 238 insertions(+), 233 deletions(-) diff --git a/src/main/java/ch/ivyteam/ivy/maven/AbstractEngineMojo.java b/src/main/java/ch/ivyteam/ivy/maven/AbstractEngineMojo.java index c0bce918..136e084d 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/AbstractEngineMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/AbstractEngineMojo.java @@ -136,7 +136,7 @@ protected final File findMatchingEngineInCacheDirectory() throws MojoExecutionEx protected final ArtifactVersion getInstalledEngineVersion(File engineDir) throws MojoExecutionException { try { - return new EngineVersionEvaluator(getLog(), engineDir).evaluateVersion(); + return new EngineVersionEvaluator(getLog(), engineDir.toPath()).evaluateVersion(); } catch (Exception ex) { throw new MojoExecutionException("Cannot evaluate engine version", ex); } diff --git a/src/main/java/ch/ivyteam/ivy/maven/InstallEngineMojo.java b/src/main/java/ch/ivyteam/ivy/maven/InstallEngineMojo.java index 64e80dfc..8327d4c5 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/InstallEngineMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/InstallEngineMojo.java @@ -19,6 +19,8 @@ import java.io.File; import java.io.IOException; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -187,7 +189,7 @@ private void downloadAndInstallEngine(boolean cleanEngineDir) throws MojoExecuti if (autoInstallEngine) { getLog().info("Will automatically download Engine now."); final EngineDownloader engineDownloader = getDownloader(); - File downloadZip = engineDownloader.downloadEngine(); + var downloadZip = engineDownloader.downloadEngine(); if (cleanEngineDir) { removeOldEngineContent(); @@ -202,7 +204,11 @@ private void downloadAndInstallEngine(boolean cleanEngineDir) throws MojoExecuti unpackEngine(downloadZip); if (!downloadUsingMaven) { - downloadZip.delete(); + try { + Files.delete(downloadZip); + } catch (IOException ex) { + throw new MojoExecutionException("Could not delete file " + downloadZip.toAbsolutePath(), ex); + } } ArtifactVersion installedEngineVersion = getInstalledEngineVersion(getRawEngineDirectory()); @@ -231,7 +237,7 @@ public EngineDownloader getDownloader() throws MojoExecutionException { @SuppressWarnings("deprecation") ProxyInfoProvider proxies = wagonManager::getProxy; return new URLEngineDownloader(engineDownloadUrl, engineListPageUrl, osArchitecture, ivyVersion, - getIvyVersionRange(), getLog(), getDownloadDirectory(), proxies); + getIvyVersionRange(), getLog(), getDownloadDirectory().toPath(), proxies); } static String ivyEngineVersionOfZip(String engineZipFileName) { @@ -258,10 +264,10 @@ private boolean engineDirectoryIsEmpty() { return !getRawEngineDirectory().isDirectory() || ArrayUtils.isEmpty(getRawEngineDirectory().listFiles()); } - private void unpackEngine(File downloadZip) throws MojoExecutionException { + private void unpackEngine(Path downloadZip) throws MojoExecutionException { String targetLocation = getRawEngineDirectory().getAbsolutePath(); - getLog().info("Unpacking engine " + downloadZip.getAbsolutePath() + " to " + targetLocation); - try (var engineZip = new ZipFile(downloadZip)) { + getLog().info("Unpacking engine " + downloadZip.toAbsolutePath() + " to " + targetLocation); + try (var engineZip = new ZipFile(downloadZip.toFile())) { engineZip.extractAll(targetLocation); } catch (IOException ex) { throw new MojoExecutionException("Failed to unpack downloaded engine '" + downloadZip + "'.", ex); @@ -271,5 +277,4 @@ private void unpackEngine(File downloadZip) throws MojoExecutionException { File getDownloadDirectory() { return SystemUtils.getJavaIoTmpDir(); } - } diff --git a/src/main/java/ch/ivyteam/ivy/maven/compile/CompileProjectMojo.java b/src/main/java/ch/ivyteam/ivy/maven/compile/CompileProjectMojo.java index 6eaab398..2df54432 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/compile/CompileProjectMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/compile/CompileProjectMojo.java @@ -32,7 +32,7 @@ /** * Compiles an ivy Project with an ivyEngine. - * + * * @author Reguel Wermelinger * @since 6.0.0 */ @@ -82,7 +82,7 @@ private void writeDependencyIarJar(Collection iarJarDepenencies) throws IO if (iarJarDepenencies == null) { // no dependencies return; } - File jar = new SharedFile(project).getIarDependencyClasspathJar(); + var jar = new SharedFile(project).getIarDependencyClasspathJar().toFile(); new ClasspathJar(jar).createFileEntries(iarJarDepenencies); } diff --git a/src/main/java/ch/ivyteam/ivy/maven/compile/CompileTestProjectMojo.java b/src/main/java/ch/ivyteam/ivy/maven/compile/CompileTestProjectMojo.java index 4e52119d..7b102e9b 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/compile/CompileTestProjectMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/compile/CompileTestProjectMojo.java @@ -32,7 +32,7 @@ /** * Compiles the test sources. - * + * * @author Reguel Wermelinger * @since 6.1.0 */ @@ -60,7 +60,7 @@ protected void engineExec(MavenProjectBuilderProxy projectBuilder) throws Except * @return persistent IAR-JARs from {@link CompileProjectMojo}. */ private List getDependencyIarJars() { - File iarJarClasspath = new SharedFile(project).getIarDependencyClasspathJar(); + var iarJarClasspath = new SharedFile(project).getIarDependencyClasspathJar().toFile(); if (!iarJarClasspath.exists()) { return Collections.emptyList(); } diff --git a/src/main/java/ch/ivyteam/ivy/maven/deploy/DeployToEngineMojo.java b/src/main/java/ch/ivyteam/ivy/maven/deploy/DeployToEngineMojo.java index adb572de..7739d882 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/deploy/DeployToEngineMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/deploy/DeployToEngineMojo.java @@ -17,6 +17,7 @@ package ch.ivyteam.ivy.maven.deploy; import java.io.File; +import java.nio.file.Path; import java.nio.file.Paths; import org.apache.commons.lang3.StringUtils; @@ -81,7 +82,7 @@ public class DeployToEngineMojo extends AbstractDeployMojo { * \\myRemoteHost\myEngineShare */ @Parameter(property = "ivy.deploy.engine.dir", defaultValue = DEPLOY_ENGINE_DIR_DEFAULT) - File deployEngineDirectory; + Path deployEngineDirectory; /** * The auto deployment directory of the engine. Must match the ivy engine @@ -195,8 +196,8 @@ private void checkHttpParams() { } Object defaultDeployEngineDirectory = project.getProperties().get(ENGINE_DIRECTORY_PROPERTY); if (deployEngineDirectory != null - && !deployEngineDirectory.getPath().equals(defaultDeployEngineDirectory)) { - logParameterIgnoredByMethod("deployEngineDirectory", deployEngineDirectory.getPath(), + && !deployEngineDirectory.toFile().getPath().equals(defaultDeployEngineDirectory)) { + logParameterIgnoredByMethod("deployEngineDirectory", deployEngineDirectory.toFile().getPath(), DeployMethod.HTTP); } } @@ -216,14 +217,13 @@ private void logParameterIgnoredByMethod(String parameter, String value, String } private File getDeployDirectory() throws MojoExecutionException { - if (deployEngineDirectory == null || engineToTarget()) { // re-use engine - // used to build - deployEngineDirectory = getEngineDir(project); + if (deployEngineDirectory == null || engineToTarget()) { // re-use engine used to build + deployEngineDirectory = getEngineDir(project).toPath(); } if (Paths.get(deployDirectory).isAbsolute()) { return new File(deployDirectory); } - return new File(deployEngineDirectory, deployDirectory); + return new File(deployEngineDirectory.toFile(), deployDirectory); } public static interface DefaultDeployOptions { diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/EngineClassLoaderFactory.java b/src/main/java/ch/ivyteam/ivy/maven/engine/EngineClassLoaderFactory.java index fe89f08e..c507da49 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/EngineClassLoaderFactory.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/EngineClassLoaderFactory.java @@ -126,7 +126,7 @@ public void writeEngineClasspathJar(File engineDirectory) throws IOException { } private void writeEngineClasspathJar(List ivyEngineClassPathFiles) throws IOException { - File classPathJar = new SharedFile(maven.project).getEngineClasspathJar(); + var classPathJar = new SharedFile(maven.project).getEngineClasspathJar().toFile(); ClasspathJar jar = new ClasspathJar(classPathJar); jar.setMainClass("ch.ivyteam.ivy.server.ServerLauncher"); jar.createFileEntries(ivyEngineClassPathFiles); diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/EngineControl.java b/src/main/java/ch/ivyteam/ivy/maven/engine/EngineControl.java index 34440809..bee2af0e 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/EngineControl.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/EngineControl.java @@ -18,8 +18,6 @@ import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URI; @@ -28,6 +26,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; +import java.nio.file.Files; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; @@ -113,13 +112,13 @@ private CommandLine toEngineCommand(Command command) { classpath += File.pathSeparator + context.vmOptions.additionalClasspath; } - File osgiDir = new File(context.engineDirectory, OsgiDir.INSTALL_AREA); + var osgiDir = context.engineDirectory.resolve(OsgiDir.INSTALL_AREA); CommandLine cli = new CommandLine(new File(getJavaExec())) .addArgument("-classpath").addArgument(classpath) .addArgument("-Divy.engine.testheadless=true") .addArgument("-Djava.awt.headless=true") - .addArgument("-Dosgi.install.area=" + osgiDir.getAbsolutePath()); + .addArgument("-Dosgi.install.area=" + osgiDir.toAbsolutePath()); if (StringUtils.isNotBlank(context.vmOptions.additionalVmOptions)) { cli.addArguments(context.vmOptions.additionalVmOptions, false); @@ -134,12 +133,12 @@ private CommandLine toEngineCommand(Command command) { private Executor createEngineExecutor() { return DefaultExecutor.builder() - .setWorkingDirectory(context.engineDirectory) + .setWorkingDirectory(context.engineDirectory.toFile()) .get(); } private PumpStreamHandler createEngineLogStreamForwarder(Consumer logLineHandler) - throws FileNotFoundException { + throws IOException { OutputStream output = getEngineLogTarget(); OutputStream engineLogStream = new LineOrientedOutputStreamRedirector(output) { @Override @@ -163,15 +162,17 @@ public void stop() throws IOException { return streamHandler; } - private OutputStream getEngineLogTarget() throws FileNotFoundException { + private OutputStream getEngineLogTarget() throws IOException { if (context.engineLogFile == null) { context.log.info("Do not forward engine output to a persistent location"); return new ByteArrayOutputStream(); } - context.properties.setMavenProperty(Property.TEST_ENGINE_LOG, context.engineLogFile.getAbsolutePath()); - context.log.info("Forwarding engine logs to: " + context.engineLogFile.getAbsolutePath()); - return new FileOutputStream(context.engineLogFile.getAbsolutePath()); + var logFile = context.engineLogFile; + var logFilePath = logFile.toAbsolutePath().toString(); + context.properties.setMavenProperty(Property.TEST_ENGINE_LOG, logFilePath); + context.log.info("Forwarding engine logs to: " + logFilePath); + return Files.newOutputStream(logFile); } private String getJavaExec() { diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/EngineModuleHints.java b/src/main/java/ch/ivyteam/ivy/maven/engine/EngineModuleHints.java index 26c9671d..d0b06b06 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/EngineModuleHints.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/EngineModuleHints.java @@ -1,8 +1,8 @@ package ch.ivyteam.ivy.maven.engine; -import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.stream.Collectors; @@ -15,12 +15,12 @@ public static void loadFromJvmOptionsFile(EngineMojoContext context, CommandLine loadJvmOptions(context.engineDirectory, context.log).stream().forEach(option -> cli.addArgument(option)); } - public static String loadFromJvmOptionsFile(File identifyAndGetEngineDirectory, Log log) { + public static String loadFromJvmOptionsFile(Path identifyAndGetEngineDirectory, Log log) { return loadJvmOptions(identifyAndGetEngineDirectory, log).stream().collect(Collectors.joining(" ", " ", " ")); } - private static List loadJvmOptions(File engineDir, Log log) { - var jvmOptionsFile = engineDir.toPath().resolve("bin").resolve("jvm-module.options"); + private static List loadJvmOptions(Path engineDir, Log log) { + var jvmOptionsFile = engineDir.resolve("bin").resolve("jvm-module.options"); if (!Files.exists(jvmOptionsFile)) { log.warn("Couldn't load jvm module options from '" + jvmOptionsFile + "' file."); return List.of(); diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/EngineMojoContext.java b/src/main/java/ch/ivyteam/ivy/maven/engine/EngineMojoContext.java index 7757cbcc..a003fe5e 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/EngineMojoContext.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/EngineMojoContext.java @@ -17,6 +17,8 @@ package ch.ivyteam.ivy.maven.engine; import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; @@ -26,16 +28,16 @@ import ch.ivyteam.ivy.maven.util.SharedFile; public class EngineMojoContext { - public final File engineDirectory; + public final Path engineDirectory; public final MavenProject project; public final Log log; public final EngineVmOptions vmOptions; public final String engineClasspathJarPath; public final MavenProperties properties; - public final File engineLogFile; + public final Path engineLogFile; public final Integer timeoutInSeconds; - public EngineMojoContext(File engineDirectory, MavenProject project, Log log, File engineLogFile, + public EngineMojoContext(Path engineDirectory, MavenProject project, Log log, Path engineLogFile, EngineVmOptions vmOptions, Integer timeoutInSeconds) { this.engineDirectory = engineDirectory; this.project = project; @@ -50,25 +52,23 @@ public EngineMojoContext(File engineDirectory, MavenProject project, Log log, Fi if (!(new File(engineClasspathJarPath).exists())) { throw new RuntimeException("Engine ClasspathJar " + engineClasspathJarPath + " does not exist."); } - if (!(engineDirectory.exists())) { + if (!Files.exists(engineDirectory)) { throw new RuntimeException("Engine Directory " + engineDirectory + " does not exist."); } } private String setupEngineClasspathJarIfNotExists() { - File classpathJar = new SharedFile(project).getEngineOSGiBootClasspathJar(); - + var classpathJar = new SharedFile(project).getEngineOSGiBootClasspathJar().toFile(); if (!classpathJar.exists()) { try { log.info("Creating a classpath jar for starting the engine"); new ClasspathJar(classpathJar) - .createFileEntries(EngineClassLoaderFactory.getOsgiBootstrapClasspath(engineDirectory)); + .createFileEntries(EngineClassLoaderFactory.getOsgiBootstrapClasspath(engineDirectory.toFile())); } catch (Exception ex) { throw new RuntimeException( "Could not create engine classpath jar: '" + classpathJar.getAbsolutePath() + "'", ex); } } - return classpathJar.getAbsolutePath(); } } diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/EngineVersionEvaluator.java b/src/main/java/ch/ivyteam/ivy/maven/engine/EngineVersionEvaluator.java index f77f9452..b3df05c1 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/EngineVersionEvaluator.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/EngineVersionEvaluator.java @@ -1,6 +1,7 @@ package ch.ivyteam.ivy.maven.engine; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; @@ -11,19 +12,20 @@ import ch.ivyteam.ivy.maven.engine.EngineClassLoaderFactory.OsgiDir; public class EngineVersionEvaluator { + public static final String LIBRARY_ID = "ch.ivyteam.util"; private Log log; - private File engineDir; + private Path engineDir; - public EngineVersionEvaluator(Log log, File engineDir) { + public EngineVersionEvaluator(Log log, Path engineDir) { this.log = log; this.engineDir = engineDir; } public ArtifactVersion evaluateVersion() { if (!isOSGiEngine(engineDir)) { - String absolutePath = engineDir == null ? "" : engineDir.getAbsolutePath(); + String absolutePath = engineDir == null ? "" : engineDir.toAbsolutePath().toString(); log.info("Can not evaluate version of a non-OSGi engine in directory '" + absolutePath + "'"); return null; } @@ -37,9 +39,9 @@ public ArtifactVersion evaluateVersion() { return new DefaultArtifactVersion(toReleaseVersion(version)); } - public static boolean isOSGiEngine(File engineDir) { - File folder = new File(engineDir, OsgiDir.INSTALL_AREA); - return folder.exists(); + public static boolean isOSGiEngine(Path engineDir) { + var folder = engineDir.resolve(OsgiDir.INSTALL_AREA); + return Files.exists(folder); } public static String toReleaseVersion(String version) { // 6.1.0.51869 -> @@ -52,12 +54,12 @@ public static String toReleaseVersion(String version) { // 6.1.0.51869 -> } private String getLibraryFileName(String libraryId) { - File ivyLibs = new File(engineDir, OsgiDir.PLUGINS); - if (!ivyLibs.exists()) { + var ivyLibs = engineDir.resolve(OsgiDir.PLUGINS); + if (!Files.exists(ivyLibs)) { return null; } - String[] libraryNames = ivyLibs.list(); + String[] libraryNames = ivyLibs.toFile().list(); if (libraryNames == null) { return null; } @@ -69,5 +71,4 @@ private String getLibraryFileName(String libraryId) { } return null; } - } diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/MavenProjectBuilderProxy.java b/src/main/java/ch/ivyteam/ivy/maven/engine/MavenProjectBuilderProxy.java index a5f8d1b3..ec50e356 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/MavenProjectBuilderProxy.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/MavenProjectBuilderProxy.java @@ -37,7 +37,7 @@ /** * Provides project build functionality that can only be accessed trough * reflection on an ivy Engine classloader. - * + * * @author Reguel Wermelinger * @since 6.0.0 */ @@ -81,7 +81,7 @@ private void logEngine() private Class getOsgiBundledDelegate(URLClassLoader ivyEngineClassLoader, int timeoutEngineStartInSeconds) throws Exception { - Object bundleContext = new OsgiRuntime(baseDirToBuildIn, log).startEclipseOsgiImpl(ivyEngineClassLoader, + Object bundleContext = new OsgiRuntime(baseDirToBuildIn.toPath(), log).startEclipseOsgiImpl(ivyEngineClassLoader, timeoutEngineStartInSeconds); hackProvokeEagerStartOfJdt(bundleContext); Object buildBundle = findBundle(bundleContext, "ch.ivyteam.ivy.dataclasses.build"); diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/OsgiRuntime.java b/src/main/java/ch/ivyteam/ivy/maven/engine/OsgiRuntime.java index b0b16983..47e465ae 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/OsgiRuntime.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/OsgiRuntime.java @@ -1,8 +1,8 @@ package ch.ivyteam.ivy.maven.engine; -import java.io.File; import java.lang.reflect.Method; import java.net.URLClassLoader; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashMap; @@ -25,10 +25,11 @@ * @since 7.0 */ class OsgiRuntime { - private final File engineDir; + + private final Path engineDir; private final Log log; - OsgiRuntime(File engineDir, Log log) { + OsgiRuntime(Path engineDir, Log log) { this.engineDir = engineDir; this.log = log; } @@ -96,9 +97,9 @@ private Map createOsgiConfigurationProps() { Map properties = new LinkedHashMap<>(); properties.put("osgi.framework.useSystemProperties", Boolean.TRUE.toString()); - properties.put("user.dir", engineDir.getAbsolutePath()); - File osgiDir = new File(engineDir, OsgiDir.INSTALL_AREA); - properties.put("osgi.install.area", osgiDir.getAbsolutePath()); + properties.put("user.dir", engineDir.toAbsolutePath().toString()); + var osgiDir = engineDir.resolve(OsgiDir.INSTALL_AREA); + properties.put("osgi.install.area", osgiDir.toAbsolutePath().toString()); properties.put("org.osgi.framework.bundle.parent", "framework"); properties.put("org.osgi.framework.bootdelegation", "javax.annotation,ch.ivyteam.ivy.boot.osgi.win,ch.ivyteam.ivy.jaas," // original diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/DeploymentOptionsFileFactory.java b/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/DeploymentOptionsFileFactory.java index 009633ed..816309a8 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/DeploymentOptionsFileFactory.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/DeploymentOptionsFileFactory.java @@ -60,5 +60,4 @@ private File getTargetFile(String fileFormat) { String targetFileName = prefix + fileFormat; return new File(deployableArtifact.getParentFile(), targetFileName); } - } diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/DeploymentFiles.java b/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/DeploymentFiles.java index 4da43318..a873dc28 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/DeploymentFiles.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/DeploymentFiles.java @@ -1,42 +1,50 @@ package ch.ivyteam.ivy.maven.engine.deploy.dir; -import java.io.File; -import java.util.Arrays; - -import org.apache.commons.io.FileUtils; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; /** * Engine status files from deployment. */ public class DeploymentFiles { + private static final String LOG = ".deploymentLog"; private static final String ERROR_LOG = ".deploymentError"; - private File deployable; + private final Path deployable; - public DeploymentFiles(File deployable) { + public DeploymentFiles(Path deployable) { this.deployable = deployable; } - File getDeployCandidate() { + Path getDeployCandidate() { return deployable; } - public File log() { - return getFile(LOG); + public Path log() { + return toFile(LOG); } - public File errorLog() { - return getFile(ERROR_LOG); + public Path errorLog() { + return toFile(ERROR_LOG); } - private File getFile(String markerExtension) { - return new File(deployable.getParent(), deployable.getName() + markerExtension); + private Path toFile(String ext) { + return deployable.resolveSibling(deployable.getFileName() + ext); } public void clearAll() { - for (String markerExtension : Arrays.asList(LOG, ERROR_LOG)) { - FileUtils.deleteQuietly(getFile(markerExtension)); + for (var ext : List.of(LOG, ERROR_LOG)) { + var file = toFile(ext); + if (Files.exists(file)) { + try { + Files.delete(file); + } catch (IOException ex) { + throw new RuntimeException("Could not delete " + file, ex); + } + } } } -} \ No newline at end of file +} diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/FileDeployer.java b/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/FileDeployer.java index 279e8720..7db6e2bf 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/FileDeployer.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/FileDeployer.java @@ -17,7 +17,7 @@ import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.function.Supplier; @@ -50,10 +50,9 @@ public FileDeployer(File deployDir, File deploymentOptions, Integer deployTimeou @Override @SuppressWarnings("hiding") public void deploy(String deployableFilePath, Log log) throws MojoExecutionException { - File deployableFile = new File(deployDir, deployableFilePath); + var deployableFile = deployDir.toPath().resolve(deployableFilePath); this.deploymentFiles = new DeploymentFiles(deployableFile); this.log = log; - deployInternal(); } @@ -71,8 +70,9 @@ private void clear() { private void initDeployment() throws MojoExecutionException { try { if (deploymentOptionsFile != null) { - File engineOption = new File(deploymentFiles.getDeployCandidate().getParentFile(), - deploymentOptionsFile.getName()); + //var engineOption = deploymentFiles.getDeployCandidate().resolveSibling(deploymentOptionsFile.getName()); + //Files.copy(deploymentOptionsFile.toPath(), engineOption); + File engineOption = new File(deploymentFiles.getDeployCandidate().getParent().toFile(), deploymentOptionsFile.getName()); FileUtils.copyFile(deploymentOptionsFile, engineOption); } } catch (IOException ex) { @@ -91,12 +91,11 @@ private void copyDeployableToEngine() throws MojoExecutionException { } private void determineDeployResult() throws MojoExecutionException { - FileLogForwarder logForwarder = new FileLogForwarder(deploymentFiles.log(), log, - new EngineLogLineHandler(log)); + var logForwarder = new FileLogForwarder(deploymentFiles.log(), log, new EngineLogLineHandler(log)); try { logForwarder.activate(); log.debug("Deployment candidate " + deploymentFiles.getDeployCandidate()); - wait(() -> !deploymentFiles.getDeployCandidate().exists(), timeoutInSeconds, TimeUnit.SECONDS); + wait(() -> !Files.exists(deploymentFiles.getDeployCandidate()), timeoutInSeconds, TimeUnit.SECONDS); } catch (TimeoutException ex) { throw new MojoExecutionException("Deployment result does not exist", ex); } finally { @@ -108,14 +107,15 @@ private void determineDeployResult() throws MojoExecutionException { } private void failOnError() throws MojoExecutionException { - if (deploymentFiles.errorLog().exists()) { + var errorLog = deploymentFiles.errorLog(); + if (Files.exists(errorLog)) { try { - log.error(FileUtils.readFileToString(deploymentFiles.errorLog(), StandardCharsets.UTF_8)); + var content = Files.readString(errorLog); + log.error(content); } catch (IOException ex) { log.error("Failed to resolve deployment error cause", ex); } - throw new MojoExecutionException( - "Deployment of '" + deploymentFiles.getDeployCandidate().getName() + "' failed!"); + throw new MojoExecutionException("Deployment of '" + deploymentFiles.getDeployCandidate().getFileName() + "' failed!"); } } diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/FileLogForwarder.java b/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/FileLogForwarder.java index c4596d82..d307df29 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/FileLogForwarder.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/deploy/dir/FileLogForwarder.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; +import java.nio.file.Path; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.IOFileFilter; @@ -33,7 +34,7 @@ * @since 6.1.0 */ class FileLogForwarder { - private final File engineLog; + private final Path engineLog; private final Log mavenLog; private FileAlterationMonitor monitor; @@ -43,7 +44,7 @@ class FileLogForwarder { * @param engineLog the log file to watch for new lines * @param mavenLog the target logger */ - FileLogForwarder(File engineLog, Log mavenLog, LogLineHandler handler) { + FileLogForwarder(Path engineLog, Log mavenLog, LogLineHandler handler) { this.engineLog = engineLog; this.mavenLog = mavenLog; this.logLineHandler = handler; @@ -52,8 +53,8 @@ class FileLogForwarder { public synchronized void activate() throws MojoExecutionException { IOFileFilter logFilter = FileFilterUtils.and( FileFilterUtils.fileFileFilter(), - FileFilterUtils.nameFileFilter(engineLog.getName())); - FileAlterationObserver fileObserver = new FileAlterationObserver(engineLog.getParent(), logFilter); + FileFilterUtils.nameFileFilter(engineLog.getFileName().toString())); + FileAlterationObserver fileObserver = new FileAlterationObserver(engineLog.getParent().toFile(), logFilter); fileObserver.addListener(new LogModificationListener()); monitor = new FileAlterationMonitor(100); monitor.addObserver(fileObserver); diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/download/EngineDownloader.java b/src/main/java/ch/ivyteam/ivy/maven/engine/download/EngineDownloader.java index b5659803..96e0274b 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/download/EngineDownloader.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/download/EngineDownloader.java @@ -1,11 +1,11 @@ package ch.ivyteam.ivy.maven.engine.download; -import java.io.File; +import java.nio.file.Path; import org.apache.maven.plugin.MojoExecutionException; public interface EngineDownloader { - File downloadEngine() throws MojoExecutionException; + Path downloadEngine() throws MojoExecutionException; String getZipFileNameFromDownloadLocation() throws MojoExecutionException; } diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/download/MavenEngineDownloader.java b/src/main/java/ch/ivyteam/ivy/maven/engine/download/MavenEngineDownloader.java index 5aa877fe..77950e99 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/download/MavenEngineDownloader.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/download/MavenEngineDownloader.java @@ -1,6 +1,6 @@ package ch.ivyteam.ivy.maven.engine.download; -import java.io.File; +import java.nio.file.Path; import java.util.List; import org.apache.maven.plugin.MojoExecutionException; @@ -46,9 +46,9 @@ private ArtifactResult resolveArtifact() throws MojoExecutionException { } @Override - public File downloadEngine() throws MojoExecutionException { + public Path downloadEngine() throws MojoExecutionException { log.info("Downloading engine " + engineArtifact.getVersion() + " using maven plugin repositories"); - return resolveArtifact().getArtifact().getFile(); + return resolveArtifact().getArtifact().getFile().toPath(); } @Override diff --git a/src/main/java/ch/ivyteam/ivy/maven/engine/download/URLEngineDownloader.java b/src/main/java/ch/ivyteam/ivy/maven/engine/download/URLEngineDownloader.java index b729ae10..b296d0fc 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/engine/download/URLEngineDownloader.java +++ b/src/main/java/ch/ivyteam/ivy/maven/engine/download/URLEngineDownloader.java @@ -1,6 +1,5 @@ package ch.ivyteam.ivy.maven.engine.download; -import java.io.File; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URI; @@ -31,12 +30,12 @@ public class URLEngineDownloader implements EngineDownloader { private final String ivyVersion; private final VersionRange ivyVersionRange; private final Log log; - private final File downloadDirectory; + private final Path downloadDirectory; private String zipFileName = null; public ProxyInfoProvider proxies; public URLEngineDownloader(URL engineDownloadUrl, URL engineListPageUrl, String osArchitecture, - String ivyVersion, VersionRange ivyVersionRange, Log log, File downloadDirectory, + String ivyVersion, VersionRange ivyVersionRange, Log log, Path downloadDirectory, ProxyInfoProvider proxies) { this.engineDownloadUrl = engineDownloadUrl; this.engineListPageUrl = engineListPageUrl; @@ -49,7 +48,7 @@ public URLEngineDownloader(URL engineDownloadUrl, URL engineListPageUrl, String } @Override - public File downloadEngine() throws MojoExecutionException { + public Path downloadEngine() throws MojoExecutionException { URL downloadUrlToUse = engineDownloadUrl; if (downloadUrlToUse == null) { downloadUrlToUse = findEngineDownloadUrlFromListPage(); @@ -72,13 +71,13 @@ private URL findEngineDownloadUrlFromListPage() throws MojoExecutionException { } } - private File downloadEngineFromUrl(URL engineUrl) throws MojoExecutionException { - File downloadZip = evaluateTargetFile(engineUrl); + private Path downloadEngineFromUrl(URL engineUrl) throws MojoExecutionException { + var downloadZip = evaluateTargetFile(engineUrl); try { log.info("Starting engine download from " + engineUrl); var repo = new Repository("engine.repo", StringUtils.substringBeforeLast(engineUrl.toExternalForm(), "/")); var resource = StringUtils.substringAfterLast(engineUrl.getPath(), "/"); - wagonDownload(repo, resource, downloadZip.toPath()); + wagonDownload(repo, resource, downloadZip); return downloadZip; } catch (Exception ex) { throw new MojoExecutionException("Failed to download engine from '" + engineUrl + "' to '" @@ -86,13 +85,13 @@ private File downloadEngineFromUrl(URL engineUrl) throws MojoExecutionException } } - private File evaluateTargetFile(URL engineUrl) { + private Path evaluateTargetFile(URL engineUrl) { zipFileName = StringUtils.substringAfterLast(engineUrl.getPath(), "/"); - File downloadZip = new File(downloadDirectory, zipFileName); + var downloadZip = downloadDirectory.resolve(zipFileName); int tempFileSuffix = 0; - while (downloadZip.exists()) { + while (Files.exists(downloadZip)) { String suffixedZipFileName = zipFileName + "." + tempFileSuffix; - downloadZip = new File(downloadDirectory, suffixedZipFileName); + downloadZip = downloadDirectory.resolve(suffixedZipFileName); tempFileSuffix++; } return downloadZip; diff --git a/src/main/java/ch/ivyteam/ivy/maven/test/AbstractIntegrationTestMojo.java b/src/main/java/ch/ivyteam/ivy/maven/test/AbstractIntegrationTestMojo.java index 4a3e2e36..511b09cc 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/test/AbstractIntegrationTestMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/test/AbstractIntegrationTestMojo.java @@ -1,6 +1,7 @@ package ch.ivyteam.ivy.maven.test; import java.io.File; +import java.nio.file.Path; import java.util.Objects; import org.apache.commons.lang3.StringUtils; @@ -37,7 +38,7 @@ public abstract class AbstractIntegrationTestMojo extends AbstractEngineMojo { public final File getEngineDir(MavenProject project) throws MojoExecutionException { if (engineToTarget()) { - return getTargetDir(project); + return getTargetDir(project).toFile(); } return findMatchingEngineInCacheDirectory(); } @@ -59,8 +60,8 @@ private boolean isCachedEngine() throws MojoExecutionException { return Objects.equals(engineDir.getParentFile(), engineCacheDirectory); } - File getTargetDir(MavenProject project) { - return new File(project.getBuild().getDirectory(), "ivyEngine"); + Path getTargetDir(MavenProject project) { + return Path.of(project.getBuild().getDirectory()).resolve("ivyEngine"); } static interface TestEngineLocation { diff --git a/src/main/java/ch/ivyteam/ivy/maven/test/SetupIntegrationTestPropertiesMojo.java b/src/main/java/ch/ivyteam/ivy/maven/test/SetupIntegrationTestPropertiesMojo.java index 66ca2c06..0f551e63 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/test/SetupIntegrationTestPropertiesMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/test/SetupIntegrationTestPropertiesMojo.java @@ -58,7 +58,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { EngineControl.Property.TEST_ENGINE_URL, EngineControl.Property.TEST_ENGINE_LOG, DeployToTestEngineMojo.Property.TEST_ENGINE_APP); - jmvArgs += EngineModuleHints.loadFromJvmOptionsFile(identifyAndGetEngineDirectory(), getLog()); + jmvArgs += EngineModuleHints.loadFromJvmOptionsFile(identifyAndGetEngineDirectory().toPath(), getLog()); props.append(FAILSAFE_ARGLINE_PROPERTY, jmvArgs); } diff --git a/src/main/java/ch/ivyteam/ivy/maven/test/SetupIvyTestPropertiesMojo.java b/src/main/java/ch/ivyteam/ivy/maven/test/SetupIvyTestPropertiesMojo.java index cd24b5e4..9e85f7fe 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/test/SetupIvyTestPropertiesMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/test/SetupIvyTestPropertiesMojo.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.IOException; import java.net.URI; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -88,23 +89,23 @@ public void execute() throws MojoExecutionException, MojoFailureException { private void setIvyProperties(MavenProperties properties) throws MojoExecutionException { SharedFile shared = new SharedFile(project); - File engineCp = shared.getEngineClasspathJar(); + var engineCp = shared.getEngineClasspathJar().toFile(); if (engineCp.exists()) { properties.setMavenProperty(Property.IVY_ENGINE_CLASSPATH, getClasspath(engineCp)); } - File iarCp = shared.getIarDependencyClasspathJar(); + var iarCp = shared.getIarDependencyClasspathJar().toFile(); if (iarCp.exists()) { properties.setMavenProperty(Property.IVY_PROJECT_IAR_CLASSPATH, getClasspath(iarCp)); } - File tstVmDir = createTestVmRuntime(); - properties.setMavenProperty(Property.IVY_TEST_VM_RUNTIME, tstVmDir.getAbsolutePath()); + var tstVmDir = createTestVmRuntime(); + properties.setMavenProperty(Property.IVY_TEST_VM_RUNTIME, tstVmDir.toAbsolutePath().toString()); } - private File createTestVmRuntime() throws MojoExecutionException { + private Path createTestVmRuntime() throws MojoExecutionException { IvyTestRuntime testRuntime = new IvyTestRuntime(); - testRuntime.setProductDir(identifyAndGetEngineDirectory()); + testRuntime.setProductDir(identifyAndGetEngineDirectory().toPath()); testRuntime.setProjectLocations(getProjects()); try { return testRuntime.store(project); @@ -134,7 +135,7 @@ private void configureMavenTestProperties(MavenProperties properties) throws Moj .map(property -> "${" + property + "}") .collect(Collectors.joining(",")); properties.setMavenProperty(Property.MAVEN_TEST_ADDITIONAL_CLASSPATH, surefireClasspath); - properties.append(Property.MAVEN_TEST_ARGLINE, EngineModuleHints.loadFromJvmOptionsFile(identifyAndGetEngineDirectory(), getLog())); + properties.append(Property.MAVEN_TEST_ARGLINE, EngineModuleHints.loadFromJvmOptionsFile(identifyAndGetEngineDirectory().toPath(), getLog())); } private void setTestOutputDirectory() { diff --git a/src/main/java/ch/ivyteam/ivy/maven/test/StartTestEngineMojo.java b/src/main/java/ch/ivyteam/ivy/maven/test/StartTestEngineMojo.java index 879e8660..633c04cc 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/test/StartTestEngineMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/test/StartTestEngineMojo.java @@ -16,7 +16,6 @@ package ch.ivyteam.ivy.maven.test; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -36,14 +35,14 @@ /** * Starts the Axon Ivy Engine for integration testing. - * + * *

* After starting the engine, this goal provides the url of the engine as * property test.engine.url. You can use this property to configure * your 'maven-failsafe-plugin' to work against this test engine. However, in an * iar-integration-test lifecycle this is already provided by the * 'ivy-integration-test-properties' goal. - * + * *

  * {@code
  *   maven-failsafe-plugin
@@ -53,7 +52,7 @@
  *   
  * }
  * 
- * + * * @since 6.2.0 */ @Mojo(name = StartTestEngineMojo.GOAL) @@ -83,7 +82,7 @@ public class StartTestEngineMojo extends AbstractIntegrationTestMojo { /** The file where the engine start is logged **/ @Parameter(property = "ivy.engine.start.log", required = false, defaultValue = "${project.build.directory}/testEngineOut.log") - File engineLogFile; + Path engineLogFile; /** The maximum amount of seconds that we wait for a engine to start */ @Parameter(property = IVY_ENGINE_START_TIMEOUT_SECONDS, defaultValue = "120") @@ -108,21 +107,21 @@ public void execute() throws MojoExecutionException, MojoFailureException { } public Executor startEngine() throws Exception { - File engineDir = identifyAndGetEngineDirectory(); + var engineDir = identifyAndGetEngineDirectory(); if (engineToTarget()) { - engineDir = copyEngineToTarget(engineDir); + engineDir = copyEngineToTarget(engineDir.toPath()).toFile(); } EngineVmOptions vmOptions = new EngineVmOptions(maxmem, additionalClasspath, additionalVmOptions); EngineControl engineControl = new EngineControl(new EngineMojoContext( - engineDir, project, getLog(), engineLogFile, vmOptions, startTimeoutInSeconds)); + engineDir.toPath(), project, getLog(), engineLogFile, vmOptions, startTimeoutInSeconds)); return engineControl.start(); } - private File copyEngineToTarget(File cachedEngineDir) { - File targetEngine = getTargetDir(project); - if (targetEngine.exists()) { + private Path copyEngineToTarget(Path cachedEngineDir) { + var targetEngine = getTargetDir(project); + if (Files.exists(targetEngine)) { getLog().warn("Skipping copy of engine to " + targetEngine + " it already exists. Use \"mvn clean\" to ensure a clean engine each cycle."); return targetEngine; @@ -132,7 +131,7 @@ private File copyEngineToTarget(File cachedEngineDir) { getLog().info("Parameter is set to " + testEngine + ", copying engine from: " + cachedEngineDir + " to " + targetEngine); - copyEngine(cachedEngineDir.toPath(), targetEngine.toPath()); + copyEngine(cachedEngineDir, targetEngine); return targetEngine; } catch (IOException ex) { getLog().warn("Could not copy engine from: " + cachedEngineDir + " to " + targetEngine, ex); diff --git a/src/main/java/ch/ivyteam/ivy/maven/test/StopTestEngineMojo.java b/src/main/java/ch/ivyteam/ivy/maven/test/StopTestEngineMojo.java index 79f8ee7c..fac24090 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/test/StopTestEngineMojo.java +++ b/src/main/java/ch/ivyteam/ivy/maven/test/StopTestEngineMojo.java @@ -16,8 +16,6 @@ package ch.ivyteam.ivy.maven.test; -import java.io.File; - import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Mojo; @@ -30,7 +28,7 @@ /** * Stops the Axon Ivy Engine after integration testing - * + * * @since 6.2.0 */ @Mojo(name = StopTestEngineMojo.GOAL) @@ -78,14 +76,14 @@ public void execute() throws MojoExecutionException, MojoFailureException { } public EngineControl createEngineController() throws MojoExecutionException { - File engineDir = getEngineDir(project); + var engineDir = getEngineDir(project); if (engineDir == null || !engineDir.exists()) { engineDir = identifyAndGetEngineDirectory(); } EngineVmOptions vmOptions = new EngineVmOptions(maxmem, additionalClasspath, additionalVmOptions); EngineControl engineControl = new EngineControl(new EngineMojoContext( - engineDir, project, getLog(), null, vmOptions, stopTimeoutInSeconds)); + engineDir.toPath(), project, getLog(), null, vmOptions, stopTimeoutInSeconds)); return engineControl; } diff --git a/src/main/java/ch/ivyteam/ivy/maven/test/bpm/IvyTestRuntime.java b/src/main/java/ch/ivyteam/ivy/maven/test/bpm/IvyTestRuntime.java index c713b032..12745984 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/test/bpm/IvyTestRuntime.java +++ b/src/main/java/ch/ivyteam/ivy/maven/test/bpm/IvyTestRuntime.java @@ -1,10 +1,9 @@ package ch.ivyteam.ivy.maven.test.bpm; -import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.Properties; import java.util.stream.Collectors; @@ -12,6 +11,7 @@ import org.apache.maven.project.MavenProject; public class IvyTestRuntime { + public static final String RUNTIME_PROPS_RESOURCE = "ivyTestRuntime.properties"; public static interface Key { @@ -21,8 +21,8 @@ public static interface Key { private final Properties props = new Properties(); - public void setProductDir(File engineDir) { - props.put(Key.PRODUCT_DIR, engineDir.getAbsolutePath()); + public void setProductDir(Path engineDir) { + props.put(Key.PRODUCT_DIR, engineDir.toAbsolutePath().toString()); } public void setProjectLocations(List locations) { @@ -34,20 +34,18 @@ public void setProjectLocations(List locations) { props.setProperty(Key.PROJECT_LOCATIONS, joinedUris); } - public File store(MavenProject project) throws IOException { - File target = new File(project.getBuild().getDirectory()); - File tstVmDir = new File(target, "ivyTestVm"); - tstVmDir.mkdir(); + public Path store(MavenProject project) throws IOException { + var tstVmDir = Path.of(project.getBuild().getDirectory()).resolve("ivyTestVm"); + Files.createDirectories(tstVmDir); store(tstVmDir); return tstVmDir; } - private File store(File dir) throws IOException { - File propsFile = new File(dir, RUNTIME_PROPS_RESOURCE); - try (OutputStream os = new FileOutputStream(propsFile)) { - props.store(os, "ivy test vm runtime properties"); + private Path store(Path dir) throws IOException { + var propsFile = dir.resolve(RUNTIME_PROPS_RESOURCE); + try (var out = Files.newOutputStream(propsFile)) { + props.store(out, "ivy test vm runtime properties"); } return propsFile; } - } diff --git a/src/main/java/ch/ivyteam/ivy/maven/util/CompilerResult.java b/src/main/java/ch/ivyteam/ivy/maven/util/CompilerResult.java index 04a2ec88..39e6850e 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/util/CompilerResult.java +++ b/src/main/java/ch/ivyteam/ivy/maven/util/CompilerResult.java @@ -16,10 +16,8 @@ package ch.ivyteam.ivy.maven.util; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; @@ -38,17 +36,17 @@ public static void store(Map result, MavenProject project) throw for (Entry entry : result.entrySet()) { properties.setProperty(entry.getKey(), entry.getValue().toString()); } - File propertyFile = new SharedFile(project).getCompileResultProperties(); - try (FileOutputStream fos = new FileOutputStream(propertyFile)) { - properties.store(fos, "ivy project build results"); + var propertyFile = new SharedFile(project).getCompileResultProperties(); + try (var out = Files.newOutputStream(propertyFile)) { + properties.store(out, "ivy project build results"); } } public static CompilerResult load(MavenProject project) throws IOException { - File propertyFile = new SharedFile(project).getCompileResultProperties(); + var propertyFile = new SharedFile(project).getCompileResultProperties(); Properties compileResults = new Properties(); - try (FileInputStream fis = new FileInputStream(propertyFile)) { - compileResults.load(fis); + try (var in = Files.newInputStream(propertyFile)) { + compileResults.load(in); } return new CompilerResult(compileResults); } @@ -65,5 +63,4 @@ public String getTestOutputDirectory() { } return result.getProperty(Result.TEST_OUTPUT_DIR); } - -} \ No newline at end of file +} diff --git a/src/main/java/ch/ivyteam/ivy/maven/util/SharedFile.java b/src/main/java/ch/ivyteam/ivy/maven/util/SharedFile.java index 90374b02..1c9c65d8 100644 --- a/src/main/java/ch/ivyteam/ivy/maven/util/SharedFile.java +++ b/src/main/java/ch/ivyteam/ivy/maven/util/SharedFile.java @@ -16,36 +16,36 @@ package ch.ivyteam.ivy.maven.util; -import java.io.File; +import java.nio.file.Path; import org.apache.maven.project.MavenProject; /** * A file that is used by multiple Mojos/Goals during the build lifecycle. - * + * * @since 6.0.3 */ public class SharedFile { - private File targetDir; + + private final Path targetDir; public SharedFile(MavenProject project) { - targetDir = new File(project.getBuild().getDirectory()); + targetDir = Path.of(project.getBuild().getDirectory()); } - public File getEngineOSGiBootClasspathJar() { - return new File(targetDir, "ivy.engine.osgi.boot.classpath.jar"); + public Path getEngineOSGiBootClasspathJar() { + return targetDir.resolve("ivy.engine.osgi.boot.classpath.jar"); } - public File getEngineClasspathJar() { - return new File(targetDir, "ivy.engine.classpath.jar"); + public Path getEngineClasspathJar() { + return targetDir.resolve("ivy.engine.classpath.jar"); } - public File getIarDependencyClasspathJar() { - return new File(targetDir, "ivy.project.dependency.classpath.jar"); + public Path getIarDependencyClasspathJar() { + return targetDir.resolve("ivy.project.dependency.classpath.jar"); } - public File getCompileResultProperties() { - return new File(targetDir, "ivy.project.compile.result.properties"); + public Path getCompileResultProperties() { + return targetDir.resolve("ivy.project.compile.result.properties"); } - } diff --git a/src/test/java/ch/ivyteam/ivy/maven/BaseEngineProjectMojoTest.java b/src/test/java/ch/ivyteam/ivy/maven/BaseEngineProjectMojoTest.java index a05d7379..cbec44a3 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/BaseEngineProjectMojoTest.java +++ b/src/test/java/ch/ivyteam/ivy/maven/BaseEngineProjectMojoTest.java @@ -179,7 +179,7 @@ protected void before() throws Throwable { } private void provideClasspathJar() throws IOException { - File cpJar = new SharedFile(project).getEngineOSGiBootClasspathJar(); + var cpJar = new SharedFile(project).getEngineOSGiBootClasspathJar().toFile(); new ClasspathJar(cpJar).createFileEntries(EngineClassLoaderFactory .getOsgiBootstrapClasspath(installUpToDateEngineRule.getMojo().getRawEngineDirectory())); } diff --git a/src/test/java/ch/ivyteam/ivy/maven/TestInstallEngineMojo.java b/src/test/java/ch/ivyteam/ivy/maven/TestInstallEngineMojo.java index 8bc523fc..61d29fc7 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/TestInstallEngineMojo.java +++ b/src/test/java/ch/ivyteam/ivy/maven/TestInstallEngineMojo.java @@ -243,10 +243,10 @@ public void testEngineDownload_overProxy() throws Exception { } downloader.proxies = this::localTestProxy; - File downloaded = downloader.downloadEngine(); + var downloaded = downloader.downloadEngine(); assertThat(downloaded) - .as("served file via proxy") - .exists(); + .as("served file via proxy") + .exists(); } private ProxyInfo localTestProxy(@SuppressWarnings("unused") String protocol) { diff --git a/src/test/java/ch/ivyteam/ivy/maven/deploy/TestDeployToEngineMojo.java b/src/test/java/ch/ivyteam/ivy/maven/deploy/TestDeployToEngineMojo.java index 721700df..f4a59f1e 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/deploy/TestDeployToEngineMojo.java +++ b/src/test/java/ch/ivyteam/ivy/maven/deploy/TestDeployToEngineMojo.java @@ -20,8 +20,8 @@ import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; @@ -34,13 +34,13 @@ import ch.ivyteam.ivy.maven.engine.deploy.dir.DeploymentFiles; public class TestDeployToEngineMojo { + @Test public void deployPackedIar() throws Throwable { DeployToEngineMojo mojo = rule.getMojo(); - File deployedIar = getTarget(mojo.deployFile, mojo); - File deploymentOptionsFile = new File(deployedIar.getParentFile(), - deployedIar.getName() + "options.yaml"); + var deployedIar = getTarget(mojo.deployFile, mojo); + var deploymentOptionsFile = deployedIar.resolveSibling(deployedIar.getFileName() + ".options.yaml"); assertThat(deployedIar).doesNotExist(); assertThat(deploymentOptionsFile).doesNotExist(); @@ -49,7 +49,7 @@ public void deployPackedIar() throws Throwable { Callable engineOperation = () -> { assertThat(deploymentOptionsFile).doesNotExist(); assertThat(deployedIar).exists(); - deployedIar.delete(); + Files.delete(deployedIar); return null; }; mockEngineDeployThread.execute(engineOperation); @@ -67,9 +67,8 @@ public void deployWithExistingOptionsFile() throws Throwable { DeployToEngineMojo mojo = rule.getMojo(); mojo.deployOptionsFile = new File("src/test/resources/options.yaml"); - File deployedIar = getTarget(mojo.deployFile, mojo); - File deploymentOptionsFile = new File(deployedIar.getParentFile(), - deployedIar.getName() + ".options.yaml"); + var deployedIar = getTarget(mojo.deployFile, mojo); + var deploymentOptionsFile = deployedIar.resolveSibling(deployedIar.getFileName() + ".options.yaml"); assertThat(deployedIar).doesNotExist(); assertThat(deploymentOptionsFile).doesNotExist(); @@ -78,9 +77,9 @@ public void deployWithExistingOptionsFile() throws Throwable { Callable engineOperation = () -> { assertThat(deploymentOptionsFile).exists(); assertThat(deploymentOptionsFile).hasContent("deployTestUsers: true\ntarget: AUTO"); - deploymentOptionsFile.delete(); + Files.delete(deploymentOptionsFile); assertThat(deployedIar).exists(); - Files.delete(deployedIar.toPath()); + Files.delete(deployedIar); return null; }; mockEngineDeployThread.execute(engineOperation); @@ -100,9 +99,8 @@ public void deployWithOptions() throws Throwable { mojo.deployTargetState = "INACTIVE"; mojo.deployTargetFileFormat = "EXPANDED"; - File deployedIar = getTarget(mojo.deployFile, mojo); - File deploymentOptionsFile = new File(deployedIar.getParentFile(), - deployedIar.getName() + ".options.yaml"); + var deployedIar = getTarget(mojo.deployFile, mojo); + var deploymentOptionsFile = deployedIar.resolveSibling(deployedIar.getFileName() + ".options.yaml"); assertThat(deployedIar).doesNotExist(); assertThat(deploymentOptionsFile).doesNotExist(); @@ -111,14 +109,15 @@ public void deployWithOptions() throws Throwable { Callable engineOperation = () -> { assertThat(deploymentOptionsFile).exists(); assertThat(deploymentOptionsFile).hasContent( - "deployTestUsers: \"TRUE\"\n" + - "target:\n" + - " version: RELEASED\n" + - " state: INACTIVE\n" + - " fileFormat: EXPANDED"); - deploymentOptionsFile.delete(); + """ + deployTestUsers: "TRUE" + target: + version: RELEASED + state: INACTIVE + fileFormat: EXPANDED"""); + Files.delete(deploymentOptionsFile); assertThat(deployedIar).exists(); - Files.delete(deployedIar.toPath()); + Files.delete(deployedIar); return null; }; mockEngineDeployThread.execute(engineOperation); @@ -134,13 +133,13 @@ public void deployWithOptions() throws Throwable { public void failOnEngineDeployError() throws Throwable { DeployToEngineMojo mojo = rule.getMojo(); DeploymentFiles markers = new DeploymentFiles(getTarget(mojo.deployFile, mojo)); - File deployedIar = getTarget(mojo.deployFile, mojo); + var deployedIar = getTarget(mojo.deployFile, mojo); DelayedOperation mockEngineDeployThread = new DelayedOperation(500, TimeUnit.MILLISECONDS); Callable engineOperation = () -> { - FileUtils.write(markers.errorLog(), "validation errors", StandardCharsets.UTF_8); + Files.writeString(markers.errorLog(), "validation errors"); assertThat(deployedIar).exists(); - deployedIar.delete(); + Files.delete(deployedIar); return null; }; @@ -155,11 +154,11 @@ public void failOnEngineDeployError() throws Throwable { } } - private static File getTarget(File iar, DeployToEngineMojo mojo) { - File deploy = new File(mojo.deployEngineDirectory, mojo.deployDirectory); - File app = new File(deploy, mojo.deployToEngineApplication); - File deployedIar = new File(app, iar.getName()); - return deployedIar; + private static Path getTarget(File iar, DeployToEngineMojo mojo) { + return mojo.deployEngineDirectory + .resolve(mojo.deployDirectory) + .resolve(mojo.deployToEngineApplication) + .resolve(iar.getName()); } @Rule @@ -169,7 +168,7 @@ private static File getTarget(File iar, DeployToEngineMojo mojo) { protected void before() throws Throwable { super.before(); - getMojo().deployEngineDirectory = createEngineDir(); + getMojo().deployEngineDirectory = createEngineDir().toPath(); getMojo().deployToEngineApplication = "TestApp"; try { @@ -191,7 +190,7 @@ private File createEngineDir() { @Override protected void after() { super.after(); - FileUtils.deleteQuietly(getMojo().deployEngineDirectory); + FileUtils.deleteQuietly(getMojo().deployEngineDirectory.toFile()); } }; diff --git a/src/test/java/ch/ivyteam/ivy/maven/deploy/TestDeployToRunningEngine.java b/src/test/java/ch/ivyteam/ivy/maven/deploy/TestDeployToRunningEngine.java index af7125ef..d99b787f 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/deploy/TestDeployToRunningEngine.java +++ b/src/test/java/ch/ivyteam/ivy/maven/deploy/TestDeployToRunningEngine.java @@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintStream; +import java.nio.file.Path; import org.apache.commons.exec.Executor; import org.apache.maven.plugin.MojoExecutionException; @@ -52,7 +53,7 @@ public void setup() throws MojoExecutionException { mojo = rule.getMojo(); deployMojo = deployRule.getMojo(); deployMojo.deployToEngineApplication = "MyTestApp"; - deployMojo.deployEngineDirectory = mojo.getEngineDir(mojo.project); + deployMojo.deployEngineDirectory = mojo.getEngineDir(mojo.project).toPath(); deployMojo.deployTimeoutInSeconds = 120; deployMojo.deployFile = new File("src/test/resources/deploy-single-7.1.0-SNAPSHOT.iar"); deployMojo.deployTestUsers = "true"; @@ -68,22 +69,17 @@ public void restoreStreams() { @Test public void canDeployIar() throws Exception { deployMojo.deployToEngineApplication = "Portal"; - - File deployedIar = getTarget(deployMojo.deployFile, deployMojo); - File deployedIarFlagFile = new File(deployedIar.getParent(), deployedIar.getName() + ".deployed"); - File deployedIarLogFile = new File(deployedIar.getParent(), deployedIar.getName() + ".deploymentLog"); - + var deployedIar = getTarget(deployMojo.deployFile, deployMojo); + var deployedIarFlagFile = deployedIar.resolveSibling(deployedIar.getFileName() + ".deployed"); + var deployedIarLogFile = deployedIar.resolveSibling(deployedIar.getFileName() + ".deploymentLog"); Executor startedProcess = null; try { startedProcess = mojo.startEngine(); - deployMojo.execute(); - assertThat(deployedIar).doesNotExist(); assertThat(deployedIarFlagFile).exists(); assertThat(deployedIarLogFile).exists(); - assertThat(linesOf(deployedIarLogFile)).haveAtLeast(1, - new Condition<>(s -> s.contains("Deploying users ..."), "")); + assertThat(linesOf(deployedIarLogFile)).haveAtLeast(1, new Condition<>(s -> s.contains("Deploying users ..."), "")); } finally { kill(startedProcess); } @@ -140,11 +136,11 @@ private void deployIarRemoteAndAssert() throws Exception, MojoExecutionException } } - private static File getTarget(File iar, DeployToEngineMojo mojo) { - File deploy = new File(mojo.deployEngineDirectory, mojo.deployDirectory); - File app = new File(deploy, mojo.deployToEngineApplication); - File deployedIar = new File(app, iar.getName()); - return deployedIar; + private static Path getTarget(File iar, DeployToEngineMojo mojo) { + return mojo.deployEngineDirectory + .resolve(mojo.deployDirectory) + .resolve(mojo.deployToEngineApplication) + .resolve(iar.getName()); } private static void kill(Executor startedProcess) { diff --git a/src/test/java/ch/ivyteam/ivy/maven/engine/TestEngineVersionEvaluator.java b/src/test/java/ch/ivyteam/ivy/maven/engine/TestEngineVersionEvaluator.java index ca73b89b..f5bd6962 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/engine/TestEngineVersionEvaluator.java +++ b/src/test/java/ch/ivyteam/ivy/maven/engine/TestEngineVersionEvaluator.java @@ -18,13 +18,13 @@ class TestEngineVersionEvaluator { @Test void isOSGiEngine_invalid() { - assertThat(EngineVersionEvaluator.isOSGiEngine(tempDir.toFile())).isFalse(); + assertThat(EngineVersionEvaluator.isOSGiEngine(tempDir)).isFalse(); } @Test void isOSGiEngine_valid() throws IOException { var systemDir = tempDir.resolve(OsgiDir.INSTALL_AREA); Files.createDirectories(systemDir); - assertThat(EngineVersionEvaluator.isOSGiEngine(tempDir.toFile())).isTrue(); + assertThat(EngineVersionEvaluator.isOSGiEngine(tempDir)).isTrue(); } } diff --git a/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/dir/TestFileLogForwarder.java b/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/dir/TestFileLogForwarder.java index 3d91dcc8..09e960da 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/dir/TestFileLogForwarder.java +++ b/src/test/java/ch/ivyteam/ivy/maven/engine/deploy/dir/TestFileLogForwarder.java @@ -40,7 +40,7 @@ void fileToMavenLog() throws Exception { var fakeEngineLog = tempDir.resolve("myProject.iar.deploymentLog"); Files.createFile(fakeEngineLog); var mavenLog = new LogCollector(); - var logForwarder = new FileLogForwarder(fakeEngineLog.toFile(), mavenLog, new EngineLogLineHandler(mavenLog)); + var logForwarder = new FileLogForwarder(fakeEngineLog, mavenLog, new EngineLogLineHandler(mavenLog)); var log = new FakeLogger(fakeEngineLog); try { diff --git a/src/test/java/ch/ivyteam/ivy/maven/test/TestSetupIvyTestPropertiesMojo.java b/src/test/java/ch/ivyteam/ivy/maven/test/TestSetupIvyTestPropertiesMojo.java index ddc22a82..573bfca3 100644 --- a/src/test/java/ch/ivyteam/ivy/maven/test/TestSetupIvyTestPropertiesMojo.java +++ b/src/test/java/ch/ivyteam/ivy/maven/test/TestSetupIvyTestPropertiesMojo.java @@ -24,6 +24,7 @@ import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -105,10 +106,10 @@ public void ivyTestRuntimeClasspathResource() throws Exception { @Test public void ivyTestRuntimeIO() throws IOException { - IvyTestRuntime rt = new IvyTestRuntime(); - rt.setProductDir(new File("/tmp/myEngine")); - File ivyTestVm = rt.store(rule.project); - assertThat(ivyTestVm.getParentFile().getName()).isEqualTo("target"); + var rt = new IvyTestRuntime(); + rt.setProductDir(Path.of("/tmp/myEngine")); + var ivyTestVm = rt.store(rule.project); + assertThat(ivyTestVm.getParent().getFileName().toString()).isEqualTo("target"); } @Rule @@ -127,7 +128,7 @@ protected void before() throws Throwable { } private void writeTestClasspathJar() throws IOException { - File classPathJar = new SharedFile(getMojo().project).getEngineClasspathJar(); + var classPathJar = new SharedFile(getMojo().project).getEngineClasspathJar().toFile(); new ClasspathJar(classPathJar).createFileEntries(Arrays.asList( Files.createTempFile("dummy", ".jar").toFile(), Files.createTempFile("dummy2", ".jar").toFile()));