How can I deploy my Spring Boot Application in Equinox OSGi Container. Getting BundleException: Error loading bundle activator - spring-boot

My Activator class:
package com.package.actprovider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceRegistration;
import com.package.Application;
import org.osgi.framework.ServiceEvent;
/**
* This class implements a simple bundle that utilizes the OSGi
* framework's event mechanism to listen for service events. Upontting
* receiving a service event, it prints out the event's details.
**/
public class Activator implements BundleActivator , ServiceListener{
private ServiceRegistration registration;
//private Application application;
/**
* Implements BundleActivator.start(). Prints
* a message and adds itself to the bundle context as a service
* listener.
* #param context the framework context for the bundle.
**/
#Override
public void start(BundleContext context)
{
System.out.println("Starting to listen for service events.++++");
context.addServiceListener(this);
}
/**
* Implements BundleActivator.stop(). Prints
* a message and removes itself from the bundle context as a
* service listener.
* #param context the framework context for the bundle.
**/
#Override
public void stop(BundleContext context)
{
context.removeServiceListener(this);
System.out.println("Stopped listening for service events.");
}
/**
* Implements ServiceListener.serviceChanged().
* Prints the details of any service event from the framework.
* #param event the fired service event.
**/
public void serviceChanged(ServiceEvent event)
{
String[] objectClass = (String[])
event.getServiceReference().getProperty("objectClass");
if (event.getType() == ServiceEvent.REGISTERED)
{
System.out.println(
"Ex1: Service of type " + objectClass[0] + " registered.");
}
else if (event.getType() == ServiceEvent.UNREGISTERING)
{
System.out.println(
"Ex1: Service of type " + objectClass[0] + " unregistered.");
}
else if (event.getType() == ServiceEvent.MODIFIED)
{
System.out.println(
"Ex1: Service of type " + objectClass[0] + " modified.");
}
}
}
MANIFEST.MF file that is read by the equinox container:
Manifest-Version: 1.0
Bundle-Description: A bundle that displays messages at startup and whe
n service events occur
Bundle-Name: Service listener example
Bundle-Version: 1.0.0
Bundle-Activator: com.package.actprovider.Activator
Bundle-Vendor: Apache Felix
Import-Package: org.osgi.framework
Created-By: 1.8.0_101 (Oracle Corporation)
Here Bundle-Activator: com.package.actprovider.Activator , but the activator.class file creating at /BOOT-INF/classes/package com.package.actprovider.Activator
my pom.xml is as follows
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.package</groupId>
<artifactId>myfoo</artifactId>
<version>0.1.0</version>
<packaging>bundle</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath />
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
</plugin>
<!-- tag::plugin[] -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.9</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId>
<configuration> <archive> <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive> </configuration> </plugin> -->
<plugin>
\
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<dependency>
<groupId>sdp-api-java</groupId>
<artifactId>sdp-api-java</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>
please suggest how can I add spring-boot bundle inside osgi container

Spring boot and OSGi are mutually exclusive. You can use spring to a degree but even that works badly.
So the best practice is to build OSGi applications based on declarative services or blueprint. You might also take a look at Apache Karaf for support of technologies like JPA, servlets ...
It will be a little more effort than in spring boot but you should be able to solve the same problems.

Related

Native Image - Spring Boot - AWS Serverless Java Container - startup error - missing ServletWebServerFactory bean

I am trying to create a native image for my Spring Boot application that also uses the AWS serverless Java container.
The Spring Boot app is a simple petstore application.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Comment for local -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<!-- Not using yaml -->
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-springboot2</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<!-- <filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/Log4j2Plugins.dat</exclude>
</excludes>
</filter>
</filters> -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>org.apache.tomcat.embed:*.jar</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Entry point
public class StreamLambdaHandler implements RequestStreamHandler {
private static SpringBootLambdaContainerHandler<HttpApiV2ProxyRequest, AwsProxyResponse> handler;
private static Logger logger = LoggerFactory.getLogger(StreamLambdaHandler.class);
public static void main(String[] args) {
}
static {
try {
logger.info("StreamLambdaHandler static init start");
//handler = SpringBootLambdaContainerHandler.getHttpApiV2ProxyHandler(SamplebootApplication.class);
handler = new SpringBootProxyHandlerBuilder<HttpApiV2ProxyRequest>()
.defaultHttpApiV2Proxy() .asyncInit()
.springBootApplication(SamplebootApplication.class) .buildAndInitialize();
logger.info("StreamLambdaHandler init -async init call ");
logger.info("StreamLambdaHandler init call invoked");
} catch (ContainerInitializationException e) {
// if we fail here. We re-throw the exception to force another cold start
e.printStackTrace();
throw new RuntimeException("Could not initialize Spring Boot application", e);
}
}
#Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
Instant start = Instant.now();
logger.info("StreamLambdaHandler handler method begin");
handler.proxyStream(inputStream, outputStream, context);
logger.info(
"StreamLambdaHandler handler method end in millisecs:" + start.until(Instant.now(), ChronoUnit.MILLIS));
}
The application works when deployed on AWS lambda. I am now trying to optimise the cold start time by converting to Native Image.
Added spring native
<dependency>
<!-- This is a mandatory dependency for your application -->
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-native</artifactId>
<version>0.11.4</version>
</dependency>
Removed shade plugin, added AOT plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-aot-maven-plugin</artifactId>
<version>0.11.4</version>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
<!-- <execution> <id>test-generate</id> <goals> <goal>test-generate</goal>
</goals> </execution> -->
</executions>
</plugin>
The executable jar - works fine in local.
Edit:
The jar fails with the same error as below when run with springAot=true.
The jar is now converted to Native Image using below steps.
rm -rf native
mkdir -p native
cd native
jar -xvf ../samplebootv2-native-0.0.1-SNAPSHOT.jar >/dev/null 2>&1
cp -R META-INF BOOT-INF/classes
native-image -H:Name=samplebootv2-native-app -cp BOOT-INF/classes:`find BOOT-INF/lib | tr '\n' ':'`
Image is build without errors. However, when the native image is excecuted it fails with following error.
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163) ~[samplebootv2-native-app:2.6.6]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577) ~[samplebootv2-native-app:5.3.18]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[samplebootv2-native-app:2.6.6]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) ~[samplebootv2-native-app:2.6.6]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) ~[samplebootv2-native-app:2.6.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[samplebootv2-native-app:2.6.6]
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:164) ~[na:na]
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.initialize(SpringBootLambdaContainerHandler.java:195) ~[samplebootv2-native-app:na]
at com.amazonaws.serverless.proxy.AsyncInitializationWrapper$AsyncInitializer.run(AsyncInitializationWrapper.java:120) ~[na:na]
at java.lang.Thread.run(Thread.java:829) ~[samplebootv2-native-app:na]
at com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:704) ~[samplebootv2-native-app:na]
at com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:202) ~[na:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:210) ~[samplebootv2-native-app:2.6.6]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) ~[samplebootv2-native-app:2.6.6]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160) ~[samplebootv2-native-app:2.6.6]
... 11 common frames omitted
I checked the generated image using graal vm visualiser and found that the classes from the aws-serverless-java-container-springboot2-1.8.jar were missing in the image
So I added the class files (only few of them) in
META-INF\native-image\org.springframework.aot\spring-aot\reflect-config.json
Also tried(with both init modes)
#InitializationHint(types =com.amazonaws.serverless.proxy.spring.embedded.ServerlessServletEmbeddedServerFactory.class, initTime = InitializationTime.RUN)
However, the same error repeats. The classes are missing in the generated image.
Could anyone provide some insights into what could be going wrong here?

Maven/Spring/MVC Web app - error cannot TalendJob (third party Talend

I'm developing a Spring/MVC/Maven Web app in Eclipse. The use case is for the app to call Talend jobs on an adhoc basis with parameters. The user enters time frame information (years, months) on a form page. Everything worked (form, validation, model, configuration, error checking, etc) until I added the required Talend jars.
Some background - I successfully created a simple Web app in Eclipse that has the same use case. For this app, I needed to place the Talend jars into the WEB-INF\lib folder. For various reasons, I need to build a Web app that uses Spring/MVC/Maven technologies. I loaded all the required Talend jars into the WEB-INF\lib folder (exactly like I did with the previous Web app). I ran a successful Maven clean install. But running the embedded Tomcat (version 7.2.2) produced this error:
[ERROR] COMPILATION ERROR:
[ERROR]C:\Documents\TalendAdHoc\src\main\java\com\validator\UserValidator.java:[13]
error: package talenddev1.job_gl_master_ad_hoc_0_3 does not exist
[ERROR]
C:\Documents\TalendAdHoc\src\main\java\com\validator\UserValidator.java:[141,7]
error: cannot find symbol.
I then followed the steps from this site:
https://cleanprogrammer.net/adding-3rd-party-jar-to-maven-projects/
to add the third party jars to Maven projects (installed the jar into the local repository, added repository and dependency into the pom.xml, etc). Running the embedded Tomcat produced this error:
[ERROR] COMPILATION ERROR: [ERROR]
C:\Documents\TalendAdHoc\src\main\java\com\validator\UserValidator.java:[155,16]
error: cannot access TalendJob
`
TalendJob` is located in the `UserValidator.java`:
job_GL_Master_Ad_Hoc TalendJob=new job_GL_Master_Ad_Hoc();
String[]
context=new String[] \{params...}
TalendJob.runJob(context);
I realize this maybe a Talend issue so I've been also working with the Talend community as well.
Any support will be greatly appreciated. Let me know if you need more information.
Thanks
Here is my pom.xml
`<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>TalendAdHoc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<!-- Spring MVC Dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Servlet Dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.talend</groupId>
<artifactId>job_gl_master_ad_hoc_0_3</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/lib/job_gl_master_ad_hoc_0_3.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.talend</groupId>
<artifactId>job_gl_refresh_transaction_ad_hoc_0_2</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}
/src/lib/job_gl_refresh_transaction_ad_hoc_0_2.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.talend</groupId>
<artifactId>job_gl_load_transaction_ad_hoc_0_2</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}
/src/lib/job_gl_load_transaction_ad_hoc_0_2.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.talend</groupId>
<artifactId>job_gl_load_summary_ad_hoc_0_1</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}
/src/lib/job_gl_load_summary_ad_hoc_0_1.jar
</systemPath>
</dependency>
<dependency>
<groupId>org.talend</groupId>
<artifactId>job_gl_load_project_ad_hoc_0_1</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}
/src/lib/job_gl_load_project_ad_hoc_0_1.jar
</systemPath>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk1.8.0_201\bin\javac.exe
</executable>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Embedded Apache Tomcat required for testing war -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>`
And here is the UserValidator.java:
`package com.validator;
import java.util.Calendar;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.model.User;
import talenddev1.job_gl_master_ad_hoc_0_3.job_GL_Master_Ad_Hoc;
#Component
public class UserValidator implements Validator {
#Override
public boolean supports(Class<?> clazz) {
return User.class.equals(clazz);
}
#Override
public void validate(Object obj, Errors err) {
User user = (User) obj;
Calendar cal = Calendar.getInstance();
set vars...
validation code...
error handling code...
}
job_GL_Master_Ad_Hoc talendJob = new job_GL_Master_Ad_Hoc();
String[] context = new String[] {
"--context_param Host_Analytics_CurrentYear=" + currentYear,
"--context_param Host_Analytics_CurrentYearStart=" +
currentYearStart,
"--context_param Host_Analytics_CurrentYearEnd=" + currentYearEnd,
"--context_param Host_Analytics_PreviousYear=" + previousYear,
"--context_param Host_Analytics_PreviousYearStart=" +
previousYearStart,
"--context_param Host_Analytics_PreviousYearEnd=" +
previousYearEnd,
"--context_param Host_Analytics_Transaction_Flag=" +
transactionFlag,
"--context_param Host_Analytics_Summary_Flag=" + summaryFlag,
"--context_param Host_Analytics_Project_Flag=" + projectFlag };
talendJob.runJob(context);
}
}`

CAS Maven Overlay And Spring Boot do not work together

I create a spring boot application from spring.io, then add CAS dependecy to pom file of project as below.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp</artifactId>
<version>${cas.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<overlays>
<overlay>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
after that start spring boot application, but CAS does not start in spring boot application.
what is the main problem in this usage of CAS?
Is the method of using the CAS at this way wrong?
_____________________________________________________________
finaly I solve my problem. I change the pom file as below
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<javaparser-core.version>3.12.0</javaparser-core.version>
<start.class>org.apereo.cas.web.CasWebApplication</start.class>
<start.class2>x.y.sso.SingleSignOnApplication</start.class2>
<h2.version>1.4.197</h2.version>
<cas.version>6.0.1</cas.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp-tomcat</artifactId>
<version>${cas.version}</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
<version>${javaparser-core.version}</version>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-core-configuration</artifactId>
<version>${cas.version}</version>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-core-configuration</artifactId>
<version>${cas.version}</version>
</dependency>
</dependencies>
<build>
<finalName>cas</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start.class2}</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!--<recompressZippedFiles>false</recompressZippedFiles>-->
<archive>
<compress>false</compress>
<manifestFile>
${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp-tomcat/META-INF/MANIFEST.MF
</manifestFile>
</archive>
<overlays>
<overlay>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-webapp-tomcat</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
then I create a Application class
#EnableDiscoveryClient
#SpringBootApplication(exclude = {
HibernateJpaAutoConfiguration.class,
JerseyAutoConfiguration.class,
JmxAutoConfiguration.class,
DataSourceAutoConfiguration.class,
DataSourceHealthIndicatorAutoConfiguration.class,
RedisAutoConfiguration.class,
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class,
CassandraAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
RedisRepositoriesAutoConfiguration.class})
#EnableConfigurationProperties({CasConfigurationProperties.class})
#EnableAsync
#EnableTransactionManagement(proxyTargetClass = true)
#EnableScheduling
public class SingleSignOnApplication {
public static void main(String[] args) {
SpringApplication.run(SingleSignOnApplication.class, args);
}
}
by this configuration, build maven goal and execution project by spring boot work correctly.
CAS is already a spring boot web application. Therefor have a look into cas-server-webapp-init-6.0.1.jar and look CasWebApplication.java.
#EnableDiscoveryClient
#SpringBootApplication(exclude={HibernateJpaAutoConfiguration.class, JerseyAutoConfiguration.class, GroovyTemplateAutoConfiguration.class, JmxAutoConfiguration.class, DataSourceAutoConfiguration.class, DataSourceHealthIndicatorAutoConfiguration.class, RedisAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, CassandraAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, RedisRepositoriesAutoConfiguration.class})
#EnableConfigurationProperties({CasConfigurationProperties.class})
#EnableAsync
#EnableTransactionManagement(proxyTargetClass=true)
#EnableScheduling
public class CasWebApplication
{
public static void main(String[] args)
{
Map<String, Object> properties = CasEmbeddedContainerUtils.getRuntimeProperties(Boolean.TRUE);
Banner banner = CasEmbeddedContainerUtils.getCasBannerInstance();
new SpringApplicationBuilder(new Class[] { CasWebApplication.class })
.banner(banner)
.web(WebApplicationType.SERVLET)
.properties(properties)
.logStartupInfo(true)
.contextClass(CasWebApplicationContext.class)
.run(args);
}
}
Remove the spring-boot-starter dependencies and CAS should start itself as a web application.
For further investigation I would recommend you to download the war from https://search.maven.org/artifact/org.apereo.cas/cas-server-webapp/6.0.1/war and inspect it with a tool like jd-gui.

Issue with Spring ServletRegistrationBean on official example

There are the following lines in the example of Spring-ws spring guide
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
ServletRegistrationBean bean = new ServletRegistrationBean();
return new ServletRegistrationBean(servlet, "/ws/*");
I get the following error
The constructor ServletRegistrationBean(MessageDispatcherServlet, String) is undefined
How can I fix this error?. What version of Spring boot I have to use?
****EDITED
This is the pom.xml. I think it's the same than the guide. I work with Eclipse.
<?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>
<groupId>org.springframework</groupId>
<artifactId>gs-producing-web-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- tag::springws[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
<!-- end::springws[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- tag::xsd[] -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.basedir}/src/main/resources/</schemaDirectory>
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
<!-- end::xsd[] -->
</plugins>
</build>
</project>
Java class
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
#EnableWs
#Configuration
public class WebServiceConfig extends WsConfigurerAdapter{
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
ServletRegistrationBean bean = new ServletRegistrationBean();
return new ServletRegistrationBean(servlet, "/ws/*");
}
Do I have to add dependencies to the pom.xml. What is the dependency wher is ServletRegistrationBean(servlet, "/ws/*");
The MetricsServlet needs to implement javax.servlet.Servlet dependency. You need to have this class in your project/classpath. To do this, declare this maven dependency:
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

Not able to integrate AspectJ with Maven

I banged my head for two days to integrate aspectj with maven, But did not work.
I am writing a simple program with two advices (which works fine in eclipse, so I am sure it's not a code side error).
I have recently started using maven for project building. Can't think of why I am not able to kick off aspectj compiler.
During compilation through maven - I get only class file for java without weaving. .aj file is not compiled.
Please Help!!!!
the first aspect file - ExceptionAspects.aj
package com.aspectSample;
public aspect ExceptionAspects {
pointcut ExceptionHandler(Exception e) : handler(Exception+) && args(e) && within(com.clickable.*);
pointcut callbeforeMethod():execution( public void HelloWorldExclude.*(..)) ;
before(Exception ex) : ExceptionHandler(ex)
{
System.out.println("Added Aspects before Exception :"+ex.toString());
}
before(): callbeforeMethod()
{
System.out.println("Aspect Before Method Call");
}
pointcut sysOutOrErrAccess() : get(* System.out) || get(* System.err);
declare error
: sysOutOrErrAccess()
: "Don't write to the console";
}
The source java file HelloWorldExclude.java
package com.aspectSample;
import java.sql.SQLException;
public class HelloWorldExclude {
private void FoulIntro(String message, String name) throws SQLException
{
throw new SQLException("WriteSomething");
}
public void GiveMeFoulIntro(String message, String name)
{
try
{
this.FoulIntro(message, name);
}catch(SQLException exp)
{
System.out.println("catching exception");
}
}
}
and the pom.xml file is
<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>
<groupId>com.clickable.HelloWorld-Maven</groupId>
<artifactId>HelloWorldAspect-Maven</artifactId>
<version>1.0.0</version>
<name>HelloWorld Aspect</name>
<packaging>jar</packaging>
<description>Hello World Code</description>
<properties>
<java-version>1.6</java-version>
<org.aspectj-version>1.6.11</org.aspectj-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
</dependencies>
</project>
A couple of things that you can try:
Explicitly provide the dependencies of the maven-aspectj plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
with the version of aspectjrt and aspectjtools matching the version of aspectj that you are using.
Do you by any chance have any of your sources in the test folder,
with the src/test folder, then to weave the aspects to the test sources you will have to explicitly mention that in the maven-aspectj plugin:
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
There was a stupid mistake in pom file.
aspectj plugin was added under element, which is just a configuration of all plugins. It doesn't tell compiler what to use.
It worked as soon as I removed plugin out of this element.
Thanks
Pankaj

Resources