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

Adding a new example based on CDI 4 #239

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -30,6 +30,7 @@ manner.
* [Example](#examples)
* [Undertow](#undertow)
* [Vert.x](#vertx)
* [CDI SE](#cdi)
* [Status](#status)
* [Further Planned Features](#further-planned-features)
* [Related Work](#related-work)
Expand Down Expand Up @@ -519,6 +520,23 @@ To run that image execute
docker run --rm -t -i -p 8080:8080 moditect/vertx-helloworld
```

## CDI

The [POM file](integrationtest/cdi/pom.xml) in _integrationtest/cdi_
shows another example. It uses Jakarta EE API (already modularized) adn adds module descriptors for
[Weld](http://vertx.io) and its dependencies.

Your can launch the build of the project and image with:

`mvn clean package`
antoinesd marked this conversation as resolved.
Show resolved Hide resolved

The `target` directory now contains a `jlink-image` directory.

You can launch the application by calling:

`target/jlink-image/bin/myApp`


## Status

ModiTect is at an early stage of development and it still has some rough edges.
Expand Down
235 changes: 235 additions & 0 deletions integrationtest/cdi/pom.xml
@@ -0,0 +1,235 @@
<!--

SPDX-License-Identifier: Apache-2.0

Copyright The original authors

Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0

-->
<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.moditect</groupId>
<artifactId>moditect-integrationtest-parent</artifactId>
<version>1.3.0-SNAPSHOT</version>
</parent>

<artifactId>moditect-integrationtest-cdi</artifactId>
<name>ModiTect Integration Test - CDI SE example</name>

<properties>
<weld.version>5.1.2.Final</weld.version>
<jakartaee-web-api.version>10.0.0</jakartaee-web-api.version>
<jboss-logging.version>3.5.3.Final</jboss-logging.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>${weld.version}</version>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-web-api</artifactId>
<version>${jakartaee-web-api.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>${jboss-logging.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should use latest which is 3.13.0. Please define a property in the parent pom that overrides this value https://github.com/moditect/moditect-org-parent/blob/b825070b22eec75ae8f965dfd7fd5e01898fc8bf/pom.xml#L56

<configuration>
<release>11</release>
<compilerArgs>
<compilerArg>--module-path</compilerArg>
<compilerArg>${project.build.directory}/modules</compilerArg>
</compilerArgs>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<executions>
<execution>
<id>copy-jakarta-modules</id>
<phase>process-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/modules</outputDirectory>
<excludeArtifactIds>jakarta.jakartaee-web-api</excludeArtifactIds>
<includeGroupIds>jakarta,org.jboss.logging
</includeGroupIds>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>${project.groupId}</groupId>
<artifactId>moditect-maven-plugin</artifactId>
<executions>
<execution>
<id>add-module-info-to-dependencies</id>
<phase>package</phase>
<goals>
<goal>add-module-info</goal>
</goals>
<configuration>
<overwriteExistingFiles>true</overwriteExistingFiles>
<jdepsExtraArgs>
<arg>--module-path</arg>
<arg>${project.build.directory}/modules</arg>
<arg>--ignore-missing-deps</arg>
</jdepsExtraArgs>
<modules>

<module>
<artifact>
<groupId>org.jboss.classfilewriter</groupId>
<artifactId>jboss-classfilewriter</artifactId>
</artifact>
<moduleInfo>
<name>jboss.classfilewriter</name>
</moduleInfo>
</module>

<module>
<artifact>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-api</artifactId>
</artifact>
<moduleInfo>
<name>weld.api</name>
</moduleInfo>
</module>

<module>
<artifact>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-spi</artifactId>
</artifact>
<moduleInfo>
<name>weld.spi</name>
</moduleInfo>
</module>

<module>
<artifact>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core-impl</artifactId>
</artifact>
<moduleInfo>
<name>weld.core.impl</name>
<requires>
!jboss.logging.annotations;
</requires>
</moduleInfo>
</module>

<module>
<artifact>
<groupId>org.jboss.weld.environment</groupId>
<artifactId>weld-environment-common</artifactId>
</artifact>
<moduleInfo>
<name>weld.environment.common</name>
<requires>
!jboss.logging.annotations;
</requires>
</moduleInfo>
</module>

<module>
<artifact>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-lite-extension-translator</artifactId>
</artifact>
<moduleInfo>
<name>weld.lite.extension.translator</name>
<requires>
!jboss.logging.annotations;
</requires>
<uses>
jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;
</uses>
</moduleInfo>
</module>

<module>
<artifact>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
</artifact>
<moduleInfo>
<name>weld.se.core</name>
<requires>
!jboss.logging.annotations;
</requires>
<opens>
org.jboss.weld.environment.se.threading;
org.jboss.weld.environment.se.contexts.activators;
</opens>
</moduleInfo>
</module>

</modules>
<module>
<mainClass>com.example.MyApplication</mainClass>

<moduleInfoSource>open module cdi.module {
requires transitive weld.se.core;
}
</moduleInfoSource>
</module>
</configuration>
</execution>
<execution>
<id>create-runtime-image</id>
<phase>package</phase>
<goals>
<goal>create-runtime-image</goal>
</goals>
<configuration>
<modulePath>
<path>${project.build.directory}/modules</path>
</modulePath>
<modules>
<module>cdi.module</module>
<module>weld.se.core</module>
</modules>
<jarInclusionPolicy>NONE</jarInclusionPolicy>
<noHeaderFiles>true</noHeaderFiles>
<noManPages>true</noManPages>
<launcher>
<name>myApp</name>
<module>cdi.module</module>
</launcher>
<compression>2</compression>
<stripDebug>true</stripDebug>
<outputDirectory>${project.build.directory}/jlink-image</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
42 changes: 42 additions & 0 deletions integrationtest/cdi/src/main/java/com/example/LogInterceptor.java
@@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright The original authors
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package com.example;

import org.jboss.logging.Logger;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Dependent
@Interceptor
@Logged
@Priority(Interceptor.Priority.LIBRARY_BEFORE)
public class LogInterceptor {

@Inject
Logger logger;

@AroundInvoke
Object timeMethod(InvocationContext context) throws Exception {
logger.info("*** before calling method " + context.getMethod().getName() + " ***\n");
try {
return context.proceed();
}
catch (Exception e) {
throw e;
}
finally {
logger.info("=== after calling method " + context.getMethod().getName() + " ===");
}

}
}
19 changes: 19 additions & 0 deletions integrationtest/cdi/src/main/java/com/example/Logged.java
@@ -0,0 +1,19 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright The original authors
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package com.example;

import java.lang.annotation.Retention;

import jakarta.interceptor.InterceptorBinding;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

@InterceptorBinding
@Retention(RUNTIME)
public @interface Logged {
}
23 changes: 23 additions & 0 deletions integrationtest/cdi/src/main/java/com/example/LoggerProducer.java
@@ -0,0 +1,23 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright The original authors
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package com.example;

import org.jboss.logging.Logger;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.enterprise.inject.spi.InjectionPoint;

@ApplicationScoped
public class LoggerProducer {

@Produces
Logger produceLogger(InjectionPoint injectionPoint) {
return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}
23 changes: 23 additions & 0 deletions integrationtest/cdi/src/main/java/com/example/MyApplication.java
@@ -0,0 +1,23 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright The original authors
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package com.example;

import jakarta.enterprise.inject.se.SeContainerInitializer;

public class MyApplication {

public static void main(String[] args) {
var initializer = SeContainerInitializer.newInstance();
initializer.addBeanClasses(SimpleBean.class, LogInterceptor.class, LoggerProducer.class);

var container = initializer.initialize();
var myBean = container.select(SimpleBean.class).get();
myBean.SimpleCall();
container.close();
}
}