Skip to content

Commit

Permalink
HSEARCH-5160 Include Lucene next dependencies in the dist package
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Dec 12, 2024
1 parent 061638b commit 528729b
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.search.build.enforcer;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.project.DefaultDependencyResolutionRequest;
import org.apache.maven.project.DependencyResolutionException;
import org.apache.maven.project.DependencyResolutionResult;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectDependenciesResolver;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.eclipse.aether.util.filter.AndDependencyFilter;
import org.eclipse.aether.util.filter.ExclusionsDependencyFilter;
import org.eclipse.aether.util.filter.ScopeDependencyFilter;

@Named("allLuceneNextDependenciesCopiedRule") // rule name - must start with lowercase character
public class AllLuceneNextDependenciesCopiedRule extends AbstractEnforcerRule {

private static final String DEPENDENCY_PLUGIN = "org.apache.maven.plugins:maven-dependency-plugin";
private static final String EXECUTION_COPY = "copy-dependencies-lucene-next";
private static final String LUCENE_NEXT_ARTIFACT_ID = "hibernate-search-backend-lucene-next";
// Inject needed Maven components
@Inject
private MavenSession session;

@Inject
private ProjectDependenciesResolver dependenciesResolver;

/**
* Rule parameter as list of items.
*/
private Set<Dependency> dependenciesToSkip;

public void execute() throws EnforcerRuleException {
Plugin plugin = session.getCurrentProject().getPlugin( DEPENDENCY_PLUGIN );
if ( plugin == null ) {
throw new EnforcerRuleException( "Project %s:%s does not configure the Dependency plugin (%s)!"
.formatted( session.getCurrentProject().getGroupId(), session.getCurrentProject().getArtifactId(),
DEPENDENCY_PLUGIN
) );
}

PluginExecution execution = plugin.getExecutionsAsMap().get( EXECUTION_COPY );
if ( execution == null ) {
throw new EnforcerRuleException( "Project %s:%s does not configure the Dependency plugin (%s) execution \"%s\"!"
.formatted( session.getCurrentProject().getGroupId(), session.getCurrentProject().getArtifactId(),
DEPENDENCY_PLUGIN, EXECUTION_COPY
) );
}

if ( execution.getConfiguration() instanceof Xpp3Dom configuration ) {
Xpp3Dom artifactItems = configuration.getChild( "artifactItems" );
if ( artifactItems == null ) {
throw new EnforcerRuleException(
"Project %s:%s does not specify the Javadoc plugin (%s) execution \"%s\" sourcepath configuration attribute!"
.formatted( session.getCurrentProject().getGroupId(),
session.getCurrentProject().getArtifactId(),
DEPENDENCY_PLUGIN, EXECUTION_COPY
) );
}

MavenProject luceneNextProject = session.getAllProjects().stream()
.filter( p -> "jar".equals( p.getPackaging() ) && LUCENE_NEXT_ARTIFACT_ID.equals( p.getArtifactId() ) )
.findFirst()
.orElseThrow(
() -> new EnforcerRuleException( "%s is not available, cannot determine required dependencies."
.formatted( LUCENE_NEXT_ARTIFACT_ID ) ) );

List<org.eclipse.aether.graph.Dependency> dependencies = resolveDependencies( luceneNextProject );
List<Dependency> redundantDependencies = new ArrayList<>();


for ( Xpp3Dom artifact : artifactItems.getChildren() ) {
if ( !dependencies
.removeIf( dep -> dep.getArtifact().getGroupId().equals( artifact.getChild( "groupId" ).getValue() )
&& dep.getArtifact().getArtifactId().equals( artifact.getChild( "artifactId" ).getValue() )
&& dep.getArtifact().getVersion().equals( artifact.getChild( "version" ).getValue() ) ) ) {
Dependency dependency = new Dependency();
dependency.setGroupId( artifact.getChild( "groupId" ).getValue() );
dependency.setArtifactId( artifact.getChild( "artifactId" ).getValue() );
dependency.setVersion( artifact.getChild( "version" ).getValue() );
redundantDependencies.add( dependency );
}
}

if ( !dependencies.isEmpty() ) {
throw new EnforcerRuleException(
"Some Lucene next backend dependencies are missing: %s".formatted( dependencies ) );
}
if ( !redundantDependencies.isEmpty() ) {
throw new EnforcerRuleException(
"Some Lucene next backend dependencies are redundant: %s".formatted( redundantDependencies ) );
}
}
}

private List<org.eclipse.aether.graph.Dependency> resolveDependencies(MavenProject luceneNextProject) {
try {
DependencyResolutionResult result = dependenciesResolver
.resolve( new DefaultDependencyResolutionRequest( luceneNextProject, session.getRepositorySession() )
.setResolutionFilter(
new AndDependencyFilter(
// we skip test dependencies
new ScopeDependencyFilter( "test" ),
// and the ones we explicitly asked to skip:
new ExclusionsDependencyFilter( dependenciesToSkip.stream()
.map( d -> d.getGroupId() + ":" + d.getArtifactId() ).toList() )
) ) );
return result.getDependencies();
}
catch (DependencyResolutionException e) {
throw new RuntimeException( e );
}
}
}
104 changes: 104 additions & 0 deletions distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,83 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<!--
The Maven assembly plugin will not "pull" the Lucene-next dependencies since
per dependency management Lucene libs are resolved to `version.org.apache.lucene` (9.x).
We copy the libs required by the Lucene next backend and include them "manually" instead.
The enforcer plugin checks that this list matches what we have in the backend itself.
-->
<execution>
<id>copy-dependencies-lucene-next</id>
<phase>generate-sources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>${version.org.apache.lucene.next}</version>
</artifactItem>
<artifactItem>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analysis-common</artifactId>
<version>${version.org.apache.lucene.next}</version>
</artifactItem>
<artifactItem>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>${version.org.apache.lucene.next}</version>
</artifactItem>
<artifactItem>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queries</artifactId>
<version>${version.org.apache.lucene.next}</version>
</artifactItem>
<artifactItem>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-sandbox</artifactId>
<version>${version.org.apache.lucene.next}</version>
</artifactItem>
<artifactItem>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-join</artifactId>
<version>${version.org.apache.lucene.next}</version>
</artifactItem>
<artifactItem>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-facet</artifactId>
<version>${version.org.apache.lucene.next}</version>
</artifactItem>
<artifactItem>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>${version.org.apache.lucene.next}</version>
</artifactItem>
<artifactItem>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-memory</artifactId>
<version>${version.org.apache.lucene.next}</version>
</artifactItem>
<artifactItem>
<groupId>com.carrotsearch</groupId>
<artifactId>hppc</artifactId>
<version>${version.com.carrotsearch.hppc}</version>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/dependencies-lucene-next</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand Down Expand Up @@ -238,10 +315,37 @@
<pathsToSkip>
<path>v5migrationhelper/engine/src/main/java</path>
<path>v5migrationhelper/orm/src/main/java</path>
<path>lucene-next/backend/lucene/src/main/java</path>
<path>v5migrationhelper/orm/target/generated-sources/annotations</path>
<path>v5migrationhelper/engine/target/generated-sources/annotations</path>
<path>lucene-next/backend/lucene/target/generated-sources/annotations</path>
</pathsToSkip>
</javadocSourcePathIncludesAllPublicArtifactsRule>
<allLuceneNextDependenciesCopiedRule>
<!-- We skip these dependencies as they are already added to the distribution through other entries: -->
<dependenciesToSkip>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.search</groupId>
<artifactId>hibernate-search-engine</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.search</groupId>
<artifactId>hibernate-search-util-common</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
</dependency>
</dependenciesToSkip>
</allLuceneNextDependenciesCopiedRule>
</rules>
</configuration>
</execution>
Expand Down
9 changes: 8 additions & 1 deletion distribution/src/main/assembly/dist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
SPDX-License-Identifier: Apache-2.0
Copyright Red Hat Inc. and Hibernate Authors
-->
<assembly>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 http://maven.apache.org/xsd/assembly-2.2.0.xsd">
<id>dist</id>
<formats>
<format>tar.gz</format>
Expand Down Expand Up @@ -180,6 +182,11 @@
</files>

<fileSets>
<fileSet>
<!-- Lucene "next" dependencies -->
<directory>target/dependencies-lucene-next</directory>
<outputDirectory>dist/backend/lucene-next</outputDirectory>
</fileSet>
<!-- Include all sources -->
<fileSet>
<directory>..</directory>
Expand Down

0 comments on commit 528729b

Please sign in to comment.