Skip to content
Open
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
86 changes: 86 additions & 0 deletions build-tools/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j</artifactId>
<version>5.2.1-SNAPSHOT</version>
</parent>
<artifactId>rdf4j-build-tools</artifactId>
<packaging>maven-plugin</packaging>
<name>RDF4J: Build Tools</name>
<properties>
<maven.plugin.annotations.version>3.13.1</maven.plugin.annotations.version>
<enforce-javaee-provided.fail>false</enforce-javaee-provided.fail>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.9.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.9.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.9.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>${maven.plugin.annotations.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.13.1</version>
<configuration>
<goalPrefix>rdf4j-build</goalPrefix>
</configuration>
<executions>
<execution>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (c) 2025 Eclipse RDF4J contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
******************************************************************************/

package org.eclipse.rdf4j.buildtools.license;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

@Mojo(name = "check-new-files", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
public class CheckNewFilesCopyrightMojo extends AbstractMojo {

private static final List<String> DEFAULT_INCLUDES = List.of("**/*.java", "**/*.kt", "**/*.kts", "**/*.scala",
"**/*.groovy", "**/*.xml", "**/*.xsd", "**/*.xsl", "**/*.properties", "**/*.sh");

@Parameter(defaultValue = "${project.basedir}", readonly = true, required = true)
private File baseDirectory;

@Parameter
private List<String> includes;

@Parameter
private List<String> excludes;

@Parameter(property = "copyrightCheck.baseRef")
private String baseReference;

@Override
public void execute() throws MojoExecutionException {
Path root = baseDirectory.toPath();
GitService gitService = new GitCommandService(root, baseReference);
List<String> effectiveIncludes = includes == null || includes.isEmpty() ? DEFAULT_INCLUDES
: new ArrayList<>(includes);
List<String> effectiveExcludes = excludes == null ? List.of() : new ArrayList<>(excludes);

NewFileCopyrightChecker checker = new NewFileCopyrightChecker(root, gitService, effectiveIncludes,
effectiveExcludes);
try {
checker.check();
} catch (CopyrightCheckException e) {
throw new MojoExecutionException(e.getMessage(), e);
} catch (IOException e) {
throw new MojoExecutionException("Failed to execute git copyright validation", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2025 Eclipse RDF4J contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
******************************************************************************/

package org.eclipse.rdf4j.buildtools.license;

public class CopyrightCheckException extends Exception {

public CopyrightCheckException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*******************************************************************************
* Copyright (c) 2025 Eclipse RDF4J contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
******************************************************************************/

package org.eclipse.rdf4j.buildtools.license;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Year;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class GitCommandService implements GitService {

private final Path repositoryRoot;
private final String baseReference;

public GitCommandService(Path repositoryRoot, String baseReference) {
this.repositoryRoot = repositoryRoot.toAbsolutePath().normalize();
this.baseReference = baseReference;
}

@Override
public Map<Path, Integer> findNewFilesWithCreationYear() throws IOException {
Optional<String> mergeBase = determineMergeBase();
List<String> newFiles = listNewFiles(mergeBase);
Map<Path, Integer> result = new LinkedHashMap<>();

for (String file : newFiles) {
if (file.isBlank()) {
continue;
}
Path relative = Path.of(file.trim());
int year = findCreationYear(relative);
result.put(relative, year);
}

return result;
}

private Optional<String> determineMergeBase() throws IOException {
Optional<String> reference = resolveBaseReference();
if (reference.isPresent()) {
GitResult mergeBase = runGit(false, "merge-base", "HEAD", reference.get());
if (mergeBase.isSuccess() && !mergeBase.lines().isEmpty()) {
return Optional.of(mergeBase.lines().get(0));
}
}

GitResult parent = runGit(false, "rev-parse", "HEAD^");
if (parent.isSuccess() && !parent.lines().isEmpty()) {
return Optional.of(parent.lines().get(0));
}

return Optional.empty();
}

private Optional<String> resolveBaseReference() throws IOException {
if (baseReference != null && !baseReference.isBlank()) {
return Optional.of(baseReference);
}

GitResult upstream = runGit(false, "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}");
if (upstream.isSuccess() && !upstream.lines().isEmpty()) {
return Optional.of(upstream.lines().get(0));
}

for (String candidate : List.of("origin/main", "origin/master", "main", "master")) {
GitResult verify = runGit(false, "rev-parse", "--verify", candidate);
if (verify.isSuccess()) {
return Optional.of(candidate);
}
}

return Optional.empty();
}

private List<String> listNewFiles(Optional<String> mergeBase) throws IOException {
GitResult diff;
if (mergeBase.isPresent()) {
diff = runGit(false, "diff", "--name-only", "--diff-filter=A", mergeBase.get() + "..HEAD");
} else {
diff = runGit(false, "show", "--name-only", "--diff-filter=A", "--pretty=format:", "HEAD");
}

if (!diff.isSuccess()) {
return List.of();
}

return diff.lines();
}

private int findCreationYear(Path relativePath) throws IOException {
String gitPath = relativePath.toString().replace('\\', '/');
GitResult log = runGit(false, "log", "--date=format:%Y", "--diff-filter=A", "--follow", "--format=%ad", "--",
gitPath);
if (log.isSuccess() && !log.lines().isEmpty()) {
return parseYear(log.lines().get(0));
}
return Year.now().getValue();
}

private GitResult runGit(boolean failOnError, String... arguments) throws IOException {
List<String> command = new ArrayList<>(arguments.length + 1);
command.add("git");
command.addAll(List.of(arguments));

ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(repositoryRoot.toFile());
builder.redirectErrorStream(true);
Process process = builder.start();

String output;
try (InputStream inputStream = process.getInputStream()) {
output = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
process.destroyForcibly();
throw e;
}

try {
int exitCode = process.waitFor();
if (exitCode != 0 && failOnError) {
throw new IOException(
"Git command failed: " + String.join(" ", command) + System.lineSeparator() + output);
}
return new GitResult(exitCode, output);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Interrupted while executing git command", e);
}
}

private int parseYear(String value) {
try {
return Integer.parseInt(value.trim());
} catch (NumberFormatException e) {
return Year.now().getValue();
}
}

private static final class GitResult {
private final int exitCode;
private final String output;

private GitResult(int exitCode, String output) {
this.exitCode = exitCode;
this.output = output;
}

boolean isSuccess() {
return exitCode == 0;
}

List<String> lines() {
return output.lines().map(String::trim).filter(line -> !line.isEmpty()).collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2025 Eclipse RDF4J contributors.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Distribution License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
******************************************************************************/

package org.eclipse.rdf4j.buildtools.license;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;

public interface GitService {

Map<Path, Integer> findNewFilesWithCreationYear() throws IOException;
}
Loading
Loading