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 8d81b94
Show file tree
Hide file tree
Showing 55 changed files with 640 additions and 605 deletions.
26 changes: 16 additions & 10 deletions src/main/java/ch/ivyteam/ivy/maven/AbstractEngineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package ch.ivyteam.ivy.maven;

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

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
Expand Down Expand Up @@ -55,15 +57,15 @@ public abstract class AbstractEngineMojo extends AbstractMojo {
* If the Engine does not yet exist, it can be automatically downloaded.
*/
@Parameter(property = ENGINE_DIRECTORY_PROPERTY)
public File engineDirectory;
public Path engineDirectory;

/**
* Location where ivy engines in required version can be extracted to.
* <p>
* If the Engine does not yet exist, it can be automatically downloaded.
*/
@Parameter(defaultValue = "${settings.localRepository}/.cache/ivy", property = "ivy.engine.cache.directory")
public File engineCacheDirectory;
public Path engineCacheDirectory;

/**
* The ivy Engine version or version-range that must be used. Must be equal or
Expand Down Expand Up @@ -95,11 +97,11 @@ public AbstractEngineMojo() {
* 'directory' could be yet invalid!
* @return the raw engine directory
*/
protected final File getRawEngineDirectory() {
protected final Path getRawEngineDirectory() {
return engineDirectory;
}

protected final File identifyAndGetEngineDirectory() throws MojoExecutionException {
protected final Path identifyAndGetEngineDirectory() throws MojoExecutionException {
if (!isEngineDirectoryIdentified()) {
engineDirectory = findMatchingEngineInCacheDirectory();
}
Expand All @@ -110,19 +112,19 @@ protected final boolean isEngineDirectoryIdentified() {
return engineDirectory != null;
}

protected final File findMatchingEngineInCacheDirectory() throws MojoExecutionException {
if (engineCacheDirectory == null || !engineCacheDirectory.exists()) {
protected final Path findMatchingEngineInCacheDirectory() throws MojoExecutionException {
if (engineCacheDirectory == null || !Files.exists(engineCacheDirectory)) {
return null;
}

File engineDirToTake = null;
ArtifactVersion versionOfEngineToTake = null;
for (File engineDirCandidate : engineCacheDirectory.listFiles()) {
for (File engineDirCandidate : engineCacheDirectory.toFile().listFiles()) {
if (!engineDirCandidate.isDirectory()) {
continue;
}

ArtifactVersion candidateVersion = getInstalledEngineVersion(engineDirCandidate);
ArtifactVersion candidateVersion = getInstalledEngineVersion(engineDirCandidate.toPath());
if (candidateVersion == null || !getIvyVersionRange().containsVersion(candidateVersion)) {
continue;
}
Expand All @@ -131,10 +133,14 @@ protected final File findMatchingEngineInCacheDirectory() throws MojoExecutionEx
versionOfEngineToTake = candidateVersion;
}
}
return engineDirToTake;
return toPath(engineDirToTake);
}

protected final ArtifactVersion getInstalledEngineVersion(File engineDir) throws MojoExecutionException {
private Path toPath(File file) {
return file == null ? null : file.toPath();
}

protected final ArtifactVersion getInstalledEngineVersion(Path engineDir) throws MojoExecutionException {
try {
return new EngineVersionEvaluator(getLog(), engineDir).evaluateVersion();
} catch (Exception ex) {
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/ch/ivyteam/ivy/maven/IarPackagingMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

import org.apache.commons.lang3.ArrayUtils;
import org.apache.maven.artifact.Artifact;
Expand Down Expand Up @@ -106,20 +107,20 @@ public static interface Defaults {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
String iarName = project.getArtifactId() + "-" + project.getVersion() + ".iar";
File iar = new File(project.getBuild().getDirectory(), iarName);
var iar = Path.of(project.getBuild().getDirectory()).resolve(iarName);
createIvyArchive(project.getBasedir(), iar);

Artifact artifact = project.getArtifact();
artifact.setFile(iar);
artifact.setFile(iar.toFile());
project.setArtifact(artifact);
getLog().info("Attached " + artifact + ".");
}

private void createIvyArchive(File projectDir, File targetIar) throws MojoExecutionException {
private void createIvyArchive(File projectDir, Path targetIar) throws MojoExecutionException {
ZipArchiver archiver = new ZipArchiver();
archiver.setDuplicateBehavior(Archiver.DUPLICATES_SKIP);
archiver.setDestFile(targetIar);
FileSetConverter fsConverter = new FileSetConverter(project.getBasedir());
archiver.setDestFile(targetIar.toFile());
FileSetConverter fsConverter = new FileSetConverter(project.getBasedir().toPath());
for (org.codehaus.plexus.archiver.FileSet fs : fsConverter.toPlexusFileSets(iarFileSets)) {
archiver.addFileSet(fs);
}
Expand All @@ -129,7 +130,7 @@ private void createIvyArchive(File projectDir, File targetIar) throws MojoExecut
try {
archiver.createArchive();
} catch (ArchiverException | IOException ex) {
throw new MojoExecutionException("Failed to create IAR: " + targetIar.getAbsolutePath(), ex);
throw new MojoExecutionException("Failed to create IAR: " + targetIar.toAbsolutePath(), ex);
}
}

Expand Down
48 changes: 32 additions & 16 deletions src/main/java/ch/ivyteam/ivy/maven/InstallEngineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

package ch.ivyteam.ivy.maven;

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 @@ -159,7 +160,12 @@ private void ensureEngineIsInstalled() throws MojoExecutionException {
handleNoInstalledEngine();
} else {
if (engineDirectoryIsEmpty()) {
getRawEngineDirectory().mkdirs();
var rawEngineDir = getRawEngineDirectory();
try {
Files.createDirectories(rawEngineDir);
} catch (IOException ex) {
throw new MojoExecutionException("Could not create directories " + rawEngineDir, ex);
}
}
ArtifactVersion installedEngineVersion = getInstalledEngineVersion(getRawEngineDirectory());

Expand Down Expand Up @@ -187,22 +193,30 @@ 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();
}

if (!isEngineDirectoryIdentified()) {
String engineZipFileName = engineDownloader.getZipFileNameFromDownloadLocation();
engineDirectory = new File(engineCacheDirectory, ivyEngineVersionOfZip(engineZipFileName));
engineDirectory.mkdirs();
engineDirectory = engineCacheDirectory.resolve(ivyEngineVersionOfZip(engineZipFileName));
try {
Files.createDirectories(engineDirectory);
} catch (IOException ex) {
throw new MojoExecutionException("Could not create directories " + engineDirectory, ex);
}
}

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 @@ -246,30 +260,32 @@ static String ivyEngineVersionOfZip(String engineZipFileName) {
}

private void removeOldEngineContent() throws MojoExecutionException {
var dir = getRawEngineDirectory();
try {
FileUtils.cleanDirectory(getRawEngineDirectory());
if (dir != null) {
FileUtils.cleanDirectory(dir.toFile());
}
} catch (IOException ex) {
throw new MojoExecutionException(
"Failed to clean outdated ivy Engine directory '" + getRawEngineDirectory() + "'.", ex);
"Failed to clean outdated ivy Engine directory '" + dir + "'.", ex);
}
}

private boolean engineDirectoryIsEmpty() {
return !getRawEngineDirectory().isDirectory() || ArrayUtils.isEmpty(getRawEngineDirectory().listFiles());
return !Files.isDirectory(getRawEngineDirectory()) || ArrayUtils.isEmpty(getRawEngineDirectory().toFile().listFiles());
}

private void unpackEngine(File downloadZip) throws MojoExecutionException {
String targetLocation = getRawEngineDirectory().getAbsolutePath();
getLog().info("Unpacking engine " + downloadZip.getAbsolutePath() + " to " + targetLocation);
try (var engineZip = new ZipFile(downloadZip)) {
private void unpackEngine(Path downloadZip) throws MojoExecutionException {
String targetLocation = getRawEngineDirectory().toAbsolutePath().toString();
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);
}
}

File getDownloadDirectory() {
return SystemUtils.getJavaIoTmpDir();
Path getDownloadDirectory() {
return SystemUtils.getJavaIoTmpDir().toPath();
}

}
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 @@ -32,7 +32,7 @@
/**
* Shares the Engine core classpath with the property:
* <code>ivy.engine.core.classpath</code>.
*
*
* @since 6.2.0
*/
@Mojo(name = ShareEngineCoreClasspathMojo.GOAL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package ch.ivyteam.ivy.maven.compile;

import java.io.File;
import java.nio.file.Path;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -42,7 +42,7 @@ public abstract class AbstractEngineInstanceMojo extends AbstractEngineMojo {
* temporary deployed.
*/
@Parameter(defaultValue = "${project.build.directory}/ivyBuildApp")
protected File buildApplicationDirectory;
protected Path buildApplicationDirectory;

/**
* Defines the timeout how long to wait for an engine start to compile.
Expand Down Expand Up @@ -80,7 +80,7 @@ public final void execute() throws MojoExecutionException, MojoFailureException
protected MavenProjectBuilderProxy getMavenProjectBuilder() throws Exception {
EngineClassLoaderFactory classLoaderFactory = getEngineClassloaderFactory();

File engineDir = identifyAndGetEngineDirectory();
var engineDir = identifyAndGetEngineDirectory();
if (builder == null) {
builder = new MavenProjectBuilderProxy(
classLoaderFactory,
Expand All @@ -92,7 +92,7 @@ protected MavenProjectBuilderProxy getMavenProjectBuilder() throws Exception {
classLoaderFactory.writeEngineClasspathJar(engineDir);
// share engine directory as property for custom follow up plugins:
if (engineDir != null) {
project.getProperties().put(AbstractEngineMojo.ENGINE_DIRECTORY_PROPERTY, engineDir.getAbsolutePath());
project.getProperties().put(AbstractEngineMojo.ENGINE_DIRECTORY_PROPERTY, engineDir.toAbsolutePath().toString());
}
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -60,7 +62,7 @@ public abstract class AbstractProjectCompileMojo extends AbstractEngineInstanceM
* @since 8.0.3
*/
@Parameter(property = "ivy.compiler.settings", defaultValue = ".settings/org.eclipse.jdt.core.prefs")
File compilerSettings;
Path compilerSettings;

/**
* Define compiler options. <br>
Expand Down Expand Up @@ -88,21 +90,22 @@ 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);
}

private File getCompilerSettings() {
if (compilerSettings.exists()) {
return compilerSettings;
if (Files.exists(compilerSettings)) {
return compilerSettings.toFile();
} else if (compilerWarnings) {
getLog().warn("Could not locate compiler settings file: " + compilerSettings
+ " continuing with default compiler settings");
}
return null;
}

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

Expand Down
Loading

0 comments on commit 8d81b94

Please sign in to comment.