use of io.quarkus.bootstrap.app.AdditionalDependency in project quarkus by quarkusio.
the class QuarkusDeployableContainer method deploy.
@SuppressWarnings("rawtypes")
@Override
public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
old = Thread.currentThread().getContextClassLoader();
if (testClass.get() == null) {
throw new IllegalStateException("Test class not available");
}
Class testJavaClass = testClass.get().getJavaClass();
// some TCK tests embed random libraries such as old versions of Jackson databind
// this breaks quarkus, so we just skip them
boolean skipLibraries = Boolean.getBoolean("io.quarkus.arquillian.skip-libraries");
try {
// Export the test archive
Path tmpLocation = Files.createTempDirectory("quarkus-arquillian-test");
deploymentLocation.set(tmpLocation);
archive.as(ExplodedExporter.class).exportExplodedInto(tmpLocation.toFile());
Path appLocation;
Set<Path> libraries = new HashSet<>();
if (archive instanceof WebArchive) {
// Quarkus does not support the WAR layout and so adapt the layout (similarly to quarkus-war-launcher)
appLocation = tmpLocation.resolve("app").toAbsolutePath();
// WEB-INF/lib -> lib/
if (!skipLibraries) {
if (Files.exists(tmpLocation.resolve("WEB-INF/lib"))) {
Files.move(tmpLocation.resolve("WEB-INF/lib"), tmpLocation.resolve("lib"));
}
}
// WEB-INF/classes -> archive/
if (Files.exists(tmpLocation.resolve("WEB-INF/classes"))) {
Files.move(tmpLocation.resolve("WEB-INF/classes"), appLocation);
} else {
Files.createDirectory(appLocation);
}
// META-INF -> archive/META-INF/
if (Files.exists(tmpLocation.resolve("META-INF"))) {
if (Files.exists(appLocation.resolve("META-INF"))) {
// Target directory not empty.
try (Stream<Path> fileTreeElements = Files.walk(tmpLocation.resolve("META-INF"), 2)) {
fileTreeElements.forEach(p -> {
try {
Files.createFile(p);
} catch (FileAlreadyExistsException faee) {
// Do Nothing
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
} else {
Files.move(tmpLocation.resolve("META-INF"), appLocation.resolve("META-INF"));
}
}
// WEB-INF -> archive/WEB-INF
if (Files.exists(tmpLocation.resolve("WEB-INF"))) {
Files.move(tmpLocation.resolve("WEB-INF"), appLocation.resolve("WEB-INF"));
}
// Collect all libraries
if (Files.exists(tmpLocation.resolve("lib"))) {
try (Stream<Path> libs = Files.walk(tmpLocation.resolve("lib"), 1)) {
libs.forEach((i) -> {
if (i.getFileName().toString().endsWith(".jar")) {
libraries.add(i);
}
});
}
}
} else {
appLocation = tmpLocation;
}
List<Consumer<BuildChainBuilder>> customizers = new ArrayList<>();
// Test class is a bean
customizers.add(new Consumer<BuildChainBuilder>() {
@Override
public void accept(BuildChainBuilder buildChainBuilder) {
buildChainBuilder.addBuildStep(new BuildStep() {
@Override
public void execute(BuildContext context) {
context.produce(AdditionalBeanBuildItem.unremovableOf(testJavaClass));
}
}).produces(AdditionalBeanBuildItem.class).build();
}
});
QuarkusBootstrap.Builder bootstrapBuilder = QuarkusBootstrap.builder().setApplicationRoot(appLocation).setProjectRoot(appLocation).setIsolateDeployment(false).setFlatClassPath(true).setMode(QuarkusBootstrap.Mode.TEST);
for (Path i : libraries) {
bootstrapBuilder.addAdditionalApplicationArchive(new AdditionalDependency(i, false, true));
}
// Path testLocation = PathTestHelper.getTestClassesLocation(testJavaClass);
// bootstrapBuilder.setProjectRoot(PathTestHelper.getTestClassesLocation(testJavaClass));
CuratedApplication curatedApplication = bootstrapBuilder.build().bootstrap();
AugmentAction augmentAction = new AugmentActionImpl(curatedApplication, customizers);
StartupAction app = augmentAction.createInitialRuntimeApplication();
RunningQuarkusApplication runningQuarkusApplication = app.run();
appClassloader.set(runningQuarkusApplication.getClassLoader());
runningApp.set(runningQuarkusApplication);
Thread.currentThread().setContextClassLoader(runningQuarkusApplication.getClassLoader());
// Instantiate the real test instance
testInstance = TestInstantiator.instantiateTest(testJavaClass, runningQuarkusApplication.getClassLoader());
// its pretty bogus
if (Boolean.getBoolean("io.quarkus.arquillian.copy-fields")) {
Class<?> dest = testInstance.getClass();
Class<?> source = testClass.get().getJavaClass();
while (source != Object.class) {
for (Field f : source.getDeclaredFields()) {
try {
if (Modifier.isStatic(f.getModifiers()) && !Modifier.isFinal(f.getModifiers())) {
Field df = dest.getDeclaredField(f.getName());
df.setAccessible(true);
f.setAccessible(true);
df.set(null, f.get(null));
}
} catch (Exception e) {
LOGGER.error("Failed to copy static field", e);
}
}
source = source.getSuperclass();
dest = dest.getSuperclass();
}
}
} catch (Throwable t) {
// clone the exception into the correct class loader
Throwable nt;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ObjectOutputStream a = new ObjectOutputStream(out)) {
a.writeObject(t);
a.close();
nt = (Throwable) new ObjectInputStream(new ByteArrayInputStream(out.toByteArray())).readObject();
} catch (Exception e) {
throw new DeploymentException("Unable to start the runtime runner", t);
}
throw new DeploymentException("Unable to start the runtime runner", nt);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
ProtocolMetaData metadata = new ProtocolMetaData();
// TODO: fix this
String testUri = TestHTTPResourceManager.getUri(runningApp.get());
System.setProperty("test.url", testUri);
URI uri = URI.create(testUri);
HTTPContext httpContext = new HTTPContext(uri.getHost(), uri.getPort());
// This is to work around https://github.com/arquillian/arquillian-core/issues/216
httpContext.add(new Servlet("dummy", "/"));
metadata.addContext(httpContext);
return metadata;
}
use of io.quarkus.bootstrap.app.AdditionalDependency in project quarkus by quarkusio.
the class ReaugmentTask method main.
public static void main(Path appRoot) throws Exception {
Path deploymentLib = appRoot.resolve(LIB).resolve(DEPLOYMENT_LIB);
Path buildSystemProps = appRoot.resolve(QUARKUS).resolve(BUILD_SYSTEM_PROPERTIES);
try (ObjectInputStream in = new ObjectInputStream(Files.newInputStream(deploymentLib.resolve(JarResultBuildStep.APPMODEL_DAT)))) {
Properties buildSystemProperties = new Properties();
try (InputStream buildIn = Files.newInputStream(buildSystemProps)) {
buildSystemProperties.load(buildIn);
}
MutableJarApplicationModel appModel = (MutableJarApplicationModel) in.readObject();
List<AdditionalDependency> additional = new ArrayList<>();
if (appModel.getUserProvidersDirectory() != null) {
// bit of a hack, but keeps things simple
System.setProperty("quarkus.package.user-providers-directory", appModel.getUserProvidersDirectory());
try (Stream<Path> files = Files.list(appRoot.resolve(appModel.getUserProvidersDirectory()))) {
files.forEach(new Consumer<Path>() {
@Override
public void accept(Path path) {
if (path.toString().endsWith(".jar")) {
additional.add(new AdditionalDependency(path, false, true));
}
}
});
}
}
final ApplicationModel existingModel = appModel.getAppModel(appRoot);
System.setProperty("quarkus.package.type", "mutable-jar");
try (CuratedApplication bootstrap = QuarkusBootstrap.builder().setAppArtifact(existingModel.getAppArtifact()).setExistingModel(existingModel).setRebuild(true).setBuildSystemProperties(buildSystemProperties).setBaseName(appModel.getBaseName()).addAdditionalApplicationArchives(additional).setApplicationRoot(existingModel.getAppArtifact().getResolvedPaths().getSinglePath()).setTargetDirectory(appRoot.getParent()).setBaseClassLoader(ReaugmentTask.class.getClassLoader()).build().bootstrap()) {
bootstrap.createAugmentor().createProductionApplication();
}
}
}
use of io.quarkus.bootstrap.app.AdditionalDependency in project quarkus by quarkusio.
the class AugmentActionImpl method runAugment.
private BuildResult runAugment(boolean firstRun, Set<String> changedResources, ClassChangeInformation classChangeInformation, ClassLoader deploymentClassLoader, Class<? extends BuildItem>... finalOutputs) {
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
QuarkusClassLoader classLoader = curatedApplication.getAugmentClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);
ProfileManager.setLaunchMode(launchMode);
ProfileManager.setRuntimeDefaultProfile(Optional.ofNullable(quarkusBootstrap.getBuildSystemProperties()).map(properties -> properties.getProperty(ProfileManager.QUARKUS_PROFILE_PROP)).orElse(null));
QuarkusAugmentor.Builder builder = QuarkusAugmentor.builder().setRoot(quarkusBootstrap.getApplicationRoot()).setClassLoader(classLoader).setTargetDir(quarkusBootstrap.getTargetDirectory()).setDeploymentClassLoader(deploymentClassLoader).setBuildSystemProperties(quarkusBootstrap.getBuildSystemProperties()).setEffectiveModel(curatedApplication.getApplicationModel());
if (quarkusBootstrap.getBaseName() != null) {
builder.setBaseName(quarkusBootstrap.getBaseName());
}
boolean auxiliaryApplication = curatedApplication.getQuarkusBootstrap().isAuxiliaryApplication();
builder.setAuxiliaryApplication(auxiliaryApplication);
builder.setAuxiliaryDevModeType(curatedApplication.getQuarkusBootstrap().isHostApplicationIsTestOnly() ? DevModeType.TEST_ONLY : (auxiliaryApplication ? DevModeType.LOCAL : null));
builder.setLaunchMode(launchMode);
builder.setDevModeType(devModeType);
builder.setTest(curatedApplication.getQuarkusBootstrap().isTest());
builder.setRebuild(quarkusBootstrap.isRebuild());
if (firstRun) {
builder.setLiveReloadState(new LiveReloadBuildItem(false, Collections.emptySet(), reloadContext, classChangeInformation));
} else {
builder.setLiveReloadState(new LiveReloadBuildItem(true, changedResources, reloadContext, classChangeInformation));
}
for (AdditionalDependency i : quarkusBootstrap.getAdditionalApplicationArchives()) {
// if it is forced as an app archive
if (i.isForceApplicationArchive()) {
builder.addAdditionalApplicationArchive(i.getResolvedPaths());
}
}
builder.excludeFromIndexing(quarkusBootstrap.getExcludeFromClassPath());
for (Consumer<BuildChainBuilder> i : chainCustomizers) {
builder.addBuildChainCustomizer(i);
}
for (Class<? extends BuildItem> i : finalOutputs) {
builder.addFinal(i);
}
try {
return builder.build().run();
} catch (Exception e) {
throw new RuntimeException(e);
}
} finally {
ProfileManager.setRuntimeDefaultProfile(null);
Thread.currentThread().setContextClassLoader(old);
QuarkusConfigFactory.setConfig(null);
}
}
use of io.quarkus.bootstrap.app.AdditionalDependency in project quarkus by quarkusio.
the class JBangAugmentorImpl method accept.
@Override
public void accept(CuratedApplication curatedApplication, Map<String, Object> resultMap) {
QuarkusClassLoader classLoader = curatedApplication.getAugmentClassLoader();
QuarkusBootstrap quarkusBootstrap = curatedApplication.getQuarkusBootstrap();
QuarkusAugmentor.Builder builder = QuarkusAugmentor.builder().setRoot(quarkusBootstrap.getApplicationRoot()).setClassLoader(classLoader).addFinal(ApplicationClassNameBuildItem.class).setTargetDir(quarkusBootstrap.getTargetDirectory()).setDeploymentClassLoader(curatedApplication.createDeploymentClassLoader()).setBuildSystemProperties(quarkusBootstrap.getBuildSystemProperties()).setEffectiveModel(curatedApplication.getApplicationModel());
if (quarkusBootstrap.getBaseName() != null) {
builder.setBaseName(quarkusBootstrap.getBaseName());
}
boolean auxiliaryApplication = curatedApplication.getQuarkusBootstrap().isAuxiliaryApplication();
builder.setAuxiliaryApplication(auxiliaryApplication);
builder.setAuxiliaryDevModeType(curatedApplication.getQuarkusBootstrap().isHostApplicationIsTestOnly() ? DevModeType.TEST_ONLY : (auxiliaryApplication ? DevModeType.LOCAL : null));
builder.setLaunchMode(LaunchMode.NORMAL);
builder.setRebuild(quarkusBootstrap.isRebuild());
builder.setLiveReloadState(new LiveReloadBuildItem(false, Collections.emptySet(), new HashMap<>(), null));
for (AdditionalDependency i : quarkusBootstrap.getAdditionalApplicationArchives()) {
// if it is forced as an app archive
if (i.isForceApplicationArchive()) {
builder.addAdditionalApplicationArchive(i.getResolvedPaths());
}
}
builder.addBuildChainCustomizer(new Consumer<BuildChainBuilder>() {
@Override
public void accept(BuildChainBuilder builder) {
final BuildStepBuilder stepBuilder = builder.addBuildStep((ctx) -> {
ctx.produce(new ProcessInheritIODisabledBuildItem());
});
stepBuilder.produces(ProcessInheritIODisabledBuildItem.class).build();
}
});
builder.excludeFromIndexing(quarkusBootstrap.getExcludeFromClassPath());
builder.addFinal(GeneratedClassBuildItem.class);
builder.addFinal(MainClassBuildItem.class);
builder.addFinal(GeneratedResourceBuildItem.class);
builder.addFinal(TransformedClassesBuildItem.class);
builder.addFinal(DeploymentResultBuildItem.class);
boolean nativeRequested = "native".equals(System.getProperty("quarkus.package.type"));
boolean containerBuildRequested = Boolean.getBoolean("quarkus.container-image.build");
if (nativeRequested) {
builder.addFinal(NativeImageBuildItem.class);
}
if (containerBuildRequested) {
// TODO: this is a bit ugly
// we don't nessesarily need these artifacts
// but if we include them it does mean that you can auto create docker images
// and deploy to kube etc
// for an ordinary build with no native and no docker this is a waste
builder.addFinal(ArtifactResultBuildItem.class);
}
try {
BuildResult buildResult = builder.build().run();
Map<String, byte[]> result = new HashMap<>();
for (GeneratedClassBuildItem i : buildResult.consumeMulti(GeneratedClassBuildItem.class)) {
result.put(i.getName().replace(".", "/") + ".class", i.getClassData());
}
for (GeneratedResourceBuildItem i : buildResult.consumeMulti(GeneratedResourceBuildItem.class)) {
result.put(i.getName(), i.getClassData());
}
for (Map.Entry<Path, Set<TransformedClassesBuildItem.TransformedClass>> entry : buildResult.consume(TransformedClassesBuildItem.class).getTransformedClassesByJar().entrySet()) {
for (TransformedClassesBuildItem.TransformedClass transformed : entry.getValue()) {
if (transformed.getData() != null) {
result.put(transformed.getFileName(), transformed.getData());
} else {
log.warn("Unable to remove resource " + transformed.getFileName() + " as this is not supported in JBangf");
}
}
}
resultMap.put("files", result);
final List<String> javaargs = new ArrayList<>();
javaargs.add("-Djava.util.logging.manager=org.jboss.logmanager.LogManager");
javaargs.add("-Djava.util.concurrent.ForkJoinPool.common.threadFactory=io.quarkus.bootstrap.forkjoin.QuarkusForkJoinWorkerThreadFactory");
resultMap.put("java-args", javaargs);
resultMap.put("main-class", buildResult.consume(MainClassBuildItem.class).getClassName());
if (nativeRequested) {
resultMap.put("native-image", buildResult.consume(NativeImageBuildItem.class).getPath());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of io.quarkus.bootstrap.app.AdditionalDependency in project quarkus by quarkusio.
the class IDELauncherImpl method launch.
public static Closeable launch(Path classesDir, Map<String, Object> context) {
System.setProperty(FORCE_COLOR_SUPPORT, "true");
// IDE's don't support raw mode
System.setProperty("quarkus.test.basic-console", "true");
final Path projectDir = BuildToolHelper.getProjectDir(classesDir);
if (projectDir == null) {
throw new IllegalStateException("Failed to locate project dir for " + classesDir);
}
try {
// todo : proper support for everything
final QuarkusBootstrap.Builder builder = QuarkusBootstrap.builder().setBaseClassLoader(IDELauncherImpl.class.getClassLoader()).setIsolateDeployment(true).setMode(QuarkusBootstrap.Mode.DEV).setTargetDirectory(classesDir.getParent());
if (BuildToolHelper.isGradleProject(classesDir)) {
final ApplicationModel quarkusModel = BuildToolHelper.enableGradleAppModelForDevMode(classesDir);
context.put(BootstrapConstants.SERIALIZED_APP_MODEL, BootstrapUtils.serializeAppModel(quarkusModel, false));
ArtifactSources mainSources = quarkusModel.getApplicationModule().getMainSources();
final Path launchingModulePath = mainSources.getSourceDirs().iterator().next().getOutputDir();
List<Path> applicationRoots = new ArrayList<>();
applicationRoots.add(launchingModulePath);
for (SourceDir resourceDir : mainSources.getResourceDirs()) {
applicationRoots.add(resourceDir.getOutputDir());
}
// Gradle uses a different output directory for classes, we override the one used by the IDE
builder.setProjectRoot(launchingModulePath).setApplicationRoot(PathsCollection.from(applicationRoots)).setTargetDirectory(quarkusModel.getApplicationModule().getBuildDir().toPath());
for (ResolvedDependency dep : quarkusModel.getDependencies()) {
final WorkspaceModule module = dep.getWorkspaceModule();
if (module == null) {
continue;
}
final ArtifactSources sources = module.getSources(dep.getClassifier());
for (SourceDir dir : sources.getSourceDirs()) {
builder.addAdditionalApplicationArchive(new AdditionalDependency(dir.getOutputDir(), true, false));
}
for (SourceDir dir : sources.getResourceDirs()) {
builder.addAdditionalApplicationArchive(new AdditionalDependency(dir.getOutputDir(), true, false));
}
}
} else {
builder.setApplicationRoot(classesDir).setProjectRoot(projectDir);
final BootstrapMavenContext mvnCtx = new BootstrapMavenContext(BootstrapMavenContext.config().setCurrentProject(projectDir.toString()));
final MavenArtifactResolver mvnResolver = new MavenArtifactResolver(mvnCtx);
builder.setMavenArtifactResolver(mvnResolver);
}
final CuratedApplication curatedApp = builder.build().bootstrap();
final Object appInstance = curatedApp.runInAugmentClassLoader("io.quarkus.deployment.dev.IDEDevModeMain", context);
return new IDELauncherImpl(curatedApp, appInstance == null ? null : appInstance instanceof Closeable ? (Closeable) appInstance : null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations