Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use java nio instead of io #469

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM maven:3.9.8-eclipse-temurin-21

RUN addgroup --gid 1000 build && adduser --uid 1000 --gid 1000 --disabled-password --gecos "" build && \
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && \
RUN curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && \
chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null && \
apt update && \
Expand Down
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 @@ -16,10 +16,8 @@

package ch.ivyteam.ivy.maven;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
Expand All @@ -32,33 +30,26 @@
/**
* Shares the Engine core classpath with the property:
* <code>ivy.engine.core.classpath</code>.
*
*
* @since 6.2.0
*/
@Mojo(name = ShareEngineCoreClasspathMojo.GOAL)
public class ShareEngineCoreClasspathMojo extends AbstractEngineMojo {

public static final String GOAL = "share-engine-core-classpath";

public static final String IVY_ENGINE_CORE_CLASSPATH_PROPERTY = "ivy.engine.core.classpath"; // Duplicated
// for
// the
// comment,
// JavaDoc
// value
// didn't
// work.
public static final String IVY_ENGINE_CORE_CLASSPATH_PROPERTY = "ivy.engine.core.classpath";

@Parameter(property = "project", required = true, readonly = true)
MavenProject project;

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

MavenProperties properties = new MavenProperties(project, getLog());
properties.setMavenProperty(IVY_ENGINE_CORE_CLASSPATH_PROPERTY, propertyValue);
var value = EngineClassLoaderFactory.getIvyEngineClassPathFiles(identifyAndGetEngineDirectory())
.stream()
.map(p -> p.toAbsolutePath().toString())
.collect(Collectors.joining(","));
var properties = new MavenProperties(project, getLog());
properties.setMavenProperty(IVY_ENGINE_CORE_CLASSPATH_PROPERTY, value);
}

}
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
Loading
Loading