Skip to content

Commit

Permalink
Use java.nio instead of java.io
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsuter committed Jul 22, 2024
1 parent 87c5425 commit d3b529d
Show file tree
Hide file tree
Showing 42 changed files with 350 additions and 331 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ch/ivyteam/ivy/maven/AbstractEngineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/ch/ivyteam/ivy/maven/InstallEngineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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());
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -271,5 +277,4 @@ private void unpackEngine(File downloadZip) throws MojoExecutionException {
File getDownloadDirectory() {
return SystemUtils.getJavaIoTmpDir();
}

}
13 changes: 6 additions & 7 deletions src/main/java/ch/ivyteam/ivy/maven/MavenDependencyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package ch.ivyteam.ivy.maven;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
Expand All @@ -35,7 +34,7 @@
/**
* Copy <a href="https://maven.apache.org/pom.html#Dependencies">maven
* dependencies</a> to a specific folder.
*
*
* <p>
* To reduce the size of your ivy archives, make sure that your dependencies are
* configured correctly:
Expand All @@ -45,7 +44,7 @@
* <li><a href="https://maven.apache.org/pom.html#exclusions">Exclude transient
* dependencies</a> which are already delivered by the core</li>
* </ul>
*
*
* @since 9.2.0
*/
@Mojo(name = MavenDependencyMojo.GOAL, requiresDependencyResolution = ResolutionScope.COMPILE)
Expand Down Expand Up @@ -79,15 +78,15 @@ protected void engineExec(MavenProjectBuilderProxy projectBuilder) throws Except
getLog().info("Maven dependecies: " + copied + " copied.");
}

private int copyDependency(Path mvnLibDir, List<File> deps) {
private int copyDependency(Path mvnLibDir, List<Path> deps) {
var count = 0;
for (var dep : deps) {
try {
Files.copy(dep.toPath(), mvnLibDir.resolve(dep.getName()));
getLog().debug("Copied dependency: " + dep.getName());
Files.copy(dep, mvnLibDir.resolve(dep.getFileName().toString()));
getLog().debug("Copied dependency: " + dep.getFileName());
count++;
} catch (FileAlreadyExistsException ex) {
getLog().debug("Ignore dependecy '" + dep.getName() + "' as it already exists at: " + mvnLibDir);
getLog().debug("Ignore dependecy '" + dep.getFileName() + "' as it already exists at: " + mvnLibDir);
} catch (IOException ex) {
getLog().warn("Couldn't copy depedency '" + deps + "' to: " + mvnLibDir, ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -88,6 +89,7 @@ protected Map<String, Object> getOptions() {

private String getDependencyClasspath() {
return StringUtils.join(getDependencies("jar").stream()
.map(Path::toFile)
.map(jar -> jar.getAbsolutePath())
.collect(Collectors.toList()), File.pathSeparatorChar);
}
Expand All @@ -102,7 +104,7 @@ private File getCompilerSettings() {
return null;
}

protected final List<File> getDependencies(String type) {
protected final List<Path> getDependencies(String type) {
return MavenRuntime.getDependencies(project, type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -32,7 +33,7 @@

/**
* Compiles an ivy Project with an ivyEngine.
*
*
* @author Reguel Wermelinger
* @since 6.0.0
*/
Expand Down Expand Up @@ -64,8 +65,8 @@ protected void engineExec(MavenProjectBuilderProxy projectBuilder) throws Except
}

getLog().info("Compiling ivy Project...");
List<File> iarDependencies = getDependencies("iar");
List<File> iarJars = projectBuilder.createIarJars(iarDependencies);
var iarDependencies = getDependencies("iar").stream().map(Path::toFile).collect(Collectors.toList());
var iarJars = projectBuilder.createIarJars(iarDependencies);
Map<String, Object> options = getOptions();
projectBuilder.compile(project.getBasedir(), iarJars, options);

Expand All @@ -82,7 +83,7 @@ private void writeDependencyIarJar(Collection<File> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

/**
* Compiles the test sources.
*
*
* @author Reguel Wermelinger
* @since 6.1.0
*/
Expand Down Expand Up @@ -60,7 +60,7 @@ protected void engineExec(MavenProjectBuilderProxy projectBuilder) throws Except
* @return persistent IAR-JARs from {@link CompileProjectMojo}.
*/
private List<File> getDependencyIarJars() {
File iarJarClasspath = new SharedFile(project).getIarDependencyClasspathJar();
var iarJarClasspath = new SharedFile(project).getIarDependencyClasspathJar().toFile();
if (!iarJarClasspath.exists()) {
return Collections.emptyList();
}
Expand Down
37 changes: 22 additions & 15 deletions src/main/java/ch/ivyteam/ivy/maven/deploy/AbstractDeployMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -46,7 +48,7 @@ public abstract class AbstractDeployMojo extends AbstractIntegrationTestMojo {
* from the {@link IarPackagingMojo#GOAL} is used.
*/
@Parameter(property = PROPERTY_IVY_DEPLOY_FILE, defaultValue = "${project.build.directory}/${project.artifactId}-${project.version}.iar")
protected File deployFile;
protected Path deployFile;

/**
* If set to <code>true</code> then test users defined in the projects are
Expand Down Expand Up @@ -151,7 +153,7 @@ public abstract class AbstractDeployMojo extends AbstractIntegrationTestMojo {
* </p>
*
* Example options file content:
*
*
* <pre>
* <code>deployTestUsers: auto
*target:
Expand All @@ -163,7 +165,7 @@ public abstract class AbstractDeployMojo extends AbstractIntegrationTestMojo {
* Inside the options file you can use property placeholders. The options file
* may look like this:
* </p>
*
*
* <pre>
* <code>deployTestUsers: ${ivy.deploy.test.users}
*target:
Expand All @@ -185,7 +187,7 @@ public abstract class AbstractDeployMojo extends AbstractIntegrationTestMojo {
* Guide</a>
*/
@Parameter(property = "ivy.deploy.options.file", required = false)
protected File deployOptionsFile;
protected Path deployOptionsFile;

@Component
private MavenFileFilter fileFilter;
Expand Down Expand Up @@ -218,7 +220,7 @@ protected final boolean checkSkip() {
getLog().info("Skipping deployment to engine.");
return true;
}
if (!deployFile.exists()) {
if (!Files.exists(deployFile)) {
getLog().warn("Skipping deployment of '" + deployFile + "' to engine. The file does not exist.");
return true;
}
Expand All @@ -234,7 +236,7 @@ protected final File createDeployOptionsFile(DeploymentOptionsFileFactory option
try {
String yamlOptions = YamlOptionsFactory.toYaml(this);
if (StringUtils.isNotBlank(yamlOptions)) {
return optionsFileFactory.createFromConfiguration(yamlOptions);
return optionsFileFactory.createFromConfiguration(yamlOptions).toFile();
}
} catch (IOException ex) {
throw new MojoExecutionException("Failed to generate YAML option", ex);
Expand All @@ -248,17 +250,22 @@ protected static final void removeTemporaryDeploymentOptionsFile(File deployment

protected final void deployToDirectory(File resolvedOptionsFile, File deployDir)
throws MojoExecutionException {
File targetDeployableFile = createTargetDeployableFile(deployDir);
String deployablePath = deployDir.toPath().relativize(targetDeployableFile.toPath()).toString();
IvyDeployer deployer = new FileDeployer(deployDir, resolvedOptionsFile, deployTimeoutInSeconds,
deployFile, targetDeployableFile);
var targetDeployableFile = createTargetDeployableFile(deployDir);
String deployablePath = deployDir.toPath().relativize(targetDeployableFile).toString();
if (resolvedOptionsFile == null) {

}
IvyDeployer deployer = new FileDeployer(deployDir.toPath(), toPath(resolvedOptionsFile), deployTimeoutInSeconds, deployFile, targetDeployableFile);
deployer.deploy(deployablePath, getLog());
}

private final File createTargetDeployableFile(File deployDir) {
File deployApp = new File(deployDir, deployToEngineApplication);
File targetDeployableFile = new File(deployApp, deployFile.getName());
return targetDeployableFile;
private Path toPath(File file) {
return file == null ? null : file.toPath();
}

}
private final Path createTargetDeployableFile(File deployDir) {
return deployDir.toPath()
.resolve(deployToEngineApplication)
.resolve(deployFile.getFileName().toString());
}
}
21 changes: 10 additions & 11 deletions src/main/java/ch/ivyteam/ivy/maven/deploy/DeployToEngineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,7 +82,7 @@ public class DeployToEngineMojo extends AbstractDeployMojo {
* <code>\\myRemoteHost\myEngineShare</code>
*/
@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
Expand Down Expand Up @@ -136,7 +137,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
"The parameter 'deployToEngineApplication' for goal " + GOAL + " is missing.");
}

File resolvedOptionsFile = createDeployOptionsFile(new DeploymentOptionsFileFactory(deployFile));
var resolvedOptionsFile = createDeployOptionsFile(new DeploymentOptionsFileFactory(deployFile));
try {
deployWithOptions(resolvedOptionsFile);
} finally {
Expand All @@ -145,7 +146,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

private void deployWithOptions(File resolvedOptionsFile) throws MojoExecutionException {
getLog().info("Deploying project " + deployFile.getName());
getLog().info("Deploying project " + deployFile.getFileName());
if (DeployMethod.DIRECTORY.equals(deployMethod)) {
deployToDirectory(resolvedOptionsFile);
} else if (DeployMethod.HTTP.equals(deployMethod)) {
Expand Down Expand Up @@ -184,8 +185,7 @@ private void deployToRestService(File resolvedOptionsFile) throws MojoExecutionE
getLog().warn("Can not load credentials from settings.xml because server '" + deployServerId
+ "' is not definied. Try to deploy with default username, password");
}
HttpDeployer httpDeployer = new HttpDeployer(secDispatcher, server,
deployEngineUrl, deployToEngineApplication, deployFile, resolvedOptionsFile);
var httpDeployer = new HttpDeployer(secDispatcher, server, deployEngineUrl, deployToEngineApplication, deployFile, resolvedOptionsFile.toPath());
httpDeployer.deploy(getLog());
}

Expand All @@ -195,8 +195,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);
}
}
Expand All @@ -216,14 +216,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 {
Expand Down
Loading

0 comments on commit d3b529d

Please sign in to comment.