Skip to content

Commit

Permalink
WIP fixing problem with PlexusContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
fabapp2 committed Sep 21, 2023
1 parent ac9b986 commit 7136f66
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static void main(String[] args) {

@Override
public void run(String... args) throws Exception {
Path baseDir = Path.of(args[0]);
Path baseDir = Path.of("/Users/fkrueger/projects/sbm-projects/spring-restbucks");
List<Resource> resources = scanner.scan(baseDir);
InMemoryExecutionContext executionContext = new InMemoryExecutionContext();
RewriteProjectParsingResult parsingResult = parser.parse(baseDir, resources, executionContext);
Expand Down
27 changes: 27 additions & 0 deletions sbm-support-rewrite-maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,32 @@
<artifactId>plexus-cipher</artifactId>
<version>${plexus-cypher.version}</version>
</dependency>
<!-- PlexusContainerException -->
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.plexus</artifactId>
<version>0.3.5</version>
</dependency>
</dependencies>
<build>
<plugins>

<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>/Users/fkrueger/.m2/repository</classpathPrefix>
<classpathLayoutType>repository</classpathLayoutType>
<mainClass>com.example.PlexusContainerApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.example;/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import org.apache.maven.Maven;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.repository.legacy.LegacyRepositorySystem;
import org.codehaus.plexus.*;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;

import java.net.URL;

/**
* @author Fabian Krüger
*/
public class PlexusContainerApp {
public static void main(String[] args) throws ComponentLookupException {
PlexusContainer plexusContainer = create();
Maven maven = plexusContainer.lookup(Maven.class);
System.out.println(maven);
RepositorySystem lookup = plexusContainer.lookup(RepositorySystem.class);
System.out.println(lookup);
}

public static PlexusContainer create() {
try {
ClassLoader parent = null;
boolean isContainerAutoWiring = false;
String containerClassPathScanning = "on";
String containerComponentVisibility = null;
URL overridingComponentsXml = null; //getClass().getClassLoader().getResource("META-INF/**/components.xml");

ContainerConfiguration configuration = new DefaultContainerConfiguration();
configuration.setAutoWiring(isContainerAutoWiring)
.setClassPathScanning(containerClassPathScanning)
.setComponentVisibility(containerComponentVisibility)
.setContainerConfigurationURL(overridingComponentsXml);

// inspired from https://github.com/jenkinsci/lib-jenkins-maven-embedder/blob/master/src/main/java/hudson/maven/MavenEmbedderUtils.java#L141
ClassWorld classWorld = new ClassWorld();
ClassRealm classRealm = new ClassRealm(classWorld, "maven", PlexusContainer.class.getClassLoader());
classRealm.setParentRealm(new ClassRealm(classWorld, "maven-parent",
parent == null ? Thread.currentThread().getContextClassLoader()
: parent));
configuration.setRealm(classRealm);

configuration.setClassWorld(classWorld);
return new DefaultPlexusContainer(configuration);
} catch (PlexusContainerException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static PlexusContainer create() {
ClassLoader parent = null;
boolean isContainerAutoWiring = false;
String containerClassPathScanning = "on";
String containerComponentVisibility = null;
String containerComponentVisibility = PlexusConstants.GLOBAL_VISIBILITY;
URL overridingComponentsXml = null; //getClass().getClassLoader().getResource("META-INF/**/components.xml");

ContainerConfiguration configuration = new DefaultContainerConfiguration();
Expand All @@ -55,13 +55,16 @@ public static PlexusContainer create() {
// inspired from https://github.com/jenkinsci/lib-jenkins-maven-embedder/blob/master/src/main/java/hudson/maven/MavenEmbedderUtils.java#L141
ClassWorld classWorld = new ClassWorld();
ClassRealm classRealm = new ClassRealm(classWorld, "maven", PlexusContainer.class.getClassLoader());
classRealm.setParentRealm(new ClassRealm(classWorld, "maven-parent",
parent == null ? Thread.currentThread().getContextClassLoader()
: parent));
ClassLoader effectiveParent = parent == null ? Thread.currentThread().getContextClassLoader() : parent;
ClassRealm parentRealm = new ClassRealm(classWorld, "maven-parent", effectiveParent);
classRealm.setParentRealm(parentRealm);
configuration.setRealm(classRealm);

configuration.setClassWorld(classWorld);
return new DefaultPlexusContainer(configuration);
DefaultPlexusContainer container = new DefaultPlexusContainer(configuration);
container.setLookupRealm(classRealm);

return container;
} catch (PlexusContainerException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/*
* Copyright 2021 - 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,8 +21,11 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.sbm.boot.autoconfigure.ScannerConfiguration;
import org.springframework.sbm.parsers.ProjectScanner;
import org.springframework.sbm.parsers.RewriteProjectParser;
import org.springframework.sbm.parsers.RewriteProjectParsingResult;
import java.nio.file.Path;
import java.nio.file.Path;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down

0 comments on commit 7136f66

Please sign in to comment.