Wildfly swarm - Page not found error - maven

This is the pom.xml file that I have
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hu.javacity.app.buildingsofcities</groupId>
<artifactId>city</artifactId>
<name>WildFly Swarm Example</name>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<version.wildfly.swarm>2018.4.1</version.wildfly.swarm>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>bom-all</artifactId>
<version>${version.wildfly.swarm}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly.swarm}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Java EE 7 dependency -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- WildFly Swarm Fractions -->
</dependencies>
</project>
And this would be HelloWorldEndpoint.java
package hu.javacity.app.buildingsofcities.city.rest;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
#Path("/hello")
public class HelloWorldEndpoint {
#GET
#Produces("text/plain")
public Response doGet() {
return Response.ok("Hello from WildFly Swarm!").build();
}
}
After running Maven, It says: "WildFly Swarm is ready" and I get no errors So im trying to write the following URL in: localhost:8080/hello The result is the following message: "not found" I tried using Edge, Firefox or Chrome, but to no success.

So there are a few problems here. First, you are missing the JAX-RS Swarm Fraction. This means that the dependencies in your pom.xml need to look like:
<dependencies>
<!-- Java EE 7 dependency -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- WildFly Swarm Fractions -->
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>jaxrs</artifactId>
</dependency>
</dependencies>
Now the second problem. In the 2018.4.1 version of Wildfly Swarm the swarm team removed the automatic generation of the javax.ws.rs.core.Application that is required for standard JAX-RS applications (see this blog for more information). If you try to start your code in 2018.4.1 you'll get a warning:
WFLYRS0015: No Servlet declaration found for JAX-RS application. In
demo.war either provide a class that extends
javax.ws.rs.core.Application or declare a servlet class in web.xml.
And your application doesn't work. So you have two choices to fix that issue. The first is to just use 2018.3.1 as it does do the auto generation. However, that's a poor fix as, going forward, Swarm will likely continue the behavior of the 2018.4.1 release. So to fix it you need to add your own javax.ws.rs.core.Application that could look something like:
package hu.javacity.app.buildingsofcities.city.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/")
public class RestApplication extends Application {
// intentionally empty
}
This sets up your JAX-RS path to be /. A common pattern is to use something like /rest or /svc but for now this will work fine for you.

Related

Spring boot: ERROR StatusLogger Log4j2 could not find a logging implementation

I trying to add log4j2 to spring boot project but it is giving below error.
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console
I have configured same using this link
https://howtodoinjava.com/spring-boot2/spring-boot-log4j2-config/
package log.demo.LogDemo;
/**
* Hello world!
*
*/
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
private static final Logger LOGGER = LogManager.getRootLogger();//Application.class);
public static void main(String[] args)
{
ApplicationContext ctx = SpringApplication.run(Application.class, args);
LOGGER.info("Info level log message");
LOGGER.debug("Debug level log message");
LOGGER.error("Error level log message");
}
}
<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>log.demo</groupId>
<artifactId>LogDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>LogDemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<!-- <exclude>application.properties</exclude> <exclude>TahitiConfig.xml</exclude>
<exclude>job_definitions.xml</exclude> -->
</excludes>
</resource>
</resources>
</build>
</project>
Don't understand what went wrong any help will be great.
Please let me know if required more information
Tried this solution but no help
Log4j2 could not find a logging implementation with Sprint Boot
Add dependency to your pom
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.8.2</version>
</dependency>
This is because for my case the log4j is printing from Elasticsearch. I have implemented logback.xml for logging.
To overcome default log4j error messages, you can override with slf4j.
I have this problem in NetBeans 8.0 when I added the jar log4j-api-2.12.2.jar.
Netbeans print this message in the console.
"StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console..."
I resolved this problem when I added log4j-core-2.12.2.0001L.jar in my project.
I supposed in Spring, just most add the dependency log4j-core in maven.
Link download JAR
https://jar-download.com/?search_box=log4j-core

repackage failed: Unable to find main class when creating a library using Spring-Boot-Starter-Parent version 2.1.2.RELEASE

I created an example resource server in spring security with a library that contains the WebSecurityConfigurerAdapter. When I update the parent spring-boot-starter-parent in the pom for the library from 2.0.8 to 2.1.2 as well as the spring-boot-maven-plugin I get the dreaded repackage failed: Unable to find main class
Version 2.0.8 of the spring-boot-starter-parent doesn't have this issue.
Update: This happens upon doing a mvn clean compile install. A version of the code with parent 2.0.8 can be found here. Just change the pom.xml spring starter parent from 2.0.8 to 2.1.2.
My Library pom is as follows.
<?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>com.example.utils.security</groupId>
<artifactId>resource-config</artifactId>
<version>1.0.1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-oauth2-resource-server -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
A class to define an annotation.
import com.example.utils.security.resource.autoconfig.ResourceServerConfig;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Import({ResourceServerConfig.class})
public #interface EnableExampleResourceServer {
}
The WebSecurityConfigurerAdapter
package com.example.utils.security.resource.autoconfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
#Configuration
#EnableWebSecurity
#Order(PriorityOrdered.HIGHEST_PRECEDENCE + 500)
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.anonymous().disable()
.logout().disable()
.formLogin().disable()
.httpBasic().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer().jwt();
}
}
I solved this problem by using maven-compiler-plugin instead of spring-boot-maven-plugin. Still to find out what makes spring-boot-maven-plugin to cause this problem though.
Maven plugin details:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
The release notes describe that the repackage goal has an identifier now to allow for easier overriding of the repackage execution.
The build above is a bit odd. If you don't want the repackage to occur, why are you defining the goal at all? Just don't define the goal and repackage will not occur as the parent only offers a default execution when the plugin is explicitly defined.
If you need to define it for a different reason, then the link above explains what you should do. Currently, you are redefining a second execution of the repackage goal.
There could be 2 possibilities -
The location of source directory is wrong. You have to use the sourceSets directive to fix this. your source directory should resemble something like src/main/java/your/package.
OR
No main() method in project (if this is not lib)

Executing unit tests (and applications) under Java-9 with Maven 3.5

I've found a lot of information about how to coax Maven to compile Java-9 source code, but I haven't run into compiler issues. Instead, I have trouble running under Java-9. Running mvn clean install will compile my code fine, but the build still fails with an exception when it runs unit tests. I can't figure out how to run either the unit tests or the application, under Java 9. I suspect that the --add-modules java.xml.bind is getting added to the compiler options but not the runtime options.
I have a simple Spring-Boot 2.0.1 project with one small java source file, one empty java test source, and a pom.xml file. Here's the exception:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
I already know that I can add a jaxb-api dependency to fix this, but that doesn't solve the more general problem of using Maven with Java 9 modules.
Version Info
When I type mvn -v, this is what I get:
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T00:58:13-07:00)
Maven home: /custom/apache-maven-3.5.2
Java version: 9.0.1, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"
(I've read that I need at least maven version 3.5, so this is supposed to work.)
What doesn't work
Here's what I've already tried, that doesn't work:
Neither of these works:
export MAVEN_OPTS="--add-modules java.xml.bind"
export MAVEN_OPTS="--add-modules=java.xml.bind"
Adding a .mvn/jvm.config file with --add-modules java.xml.bind didn't work.
I didn't expect it to help to add the maven compiler plug-in to the pom.xml file, because compiling the code isn't an issue. I tried it anyway, but it didn't help. This is what I added to the project/build/plugins portion of the pom.xml file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>--add-modules=java.xml.bind</arg>
</compilerArgs>
<source>1.9</source>
<target>1.9</target>
<fork>true</fork>
</configuration>
<version>3.7.0</version>
</plugin>
Building under Java 10 didn't help at all.
Source Code
I have an empty application.properties file.
My source file looks like this:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class AngularSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(AngularSpringBootApplication.class, args);
}
}
My test file looks like this, in the same package on the test tree:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class AngularSpringBootApplicationTests {
#Test
public void contextLoads() {
}
}
Here's my pom.xml file, which comes from the Spring Boot generator at http://start.spring.io/ (Some of this isn't necessary. This is a stripped-down version of a slightly larger project.)
<?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>com.neptunedreams.tutorial</groupId>
<artifactId>SpringBootBuildBug</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootBuildBug</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>9</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--This is the solution to the Jax bug at execution time, but I don't like it. -->
<!--<dependency>-->
<!--<groupId>javax.xml.bind</groupId>-->
<!--<artifactId>jaxb-api</artifactId>-->
<!--<version>2.3.0</version>-->
<!--</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-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>--add-modules=java.xml.bind</arg>
</compilerArgs>
<source>1.9</source>
<target>1.9</target>
<fork>true</fork>
</configuration>
<version>3.7.0</version>
</plugin>
</plugins>
</build>
</project>
It turns out there was a simple fix. I didn't have the latest version of Maven. When I upgraded from 3.5.2 to 3.5.3, the problem went away.

spring boot + jboss = page 403

I was trying to connect spring boot with jboss for a week... I read many topics on this forum and many of them are too old. I get error 404 every time when i want run server. Can you please help me to config my app with jboss properly?
UPDATE:
Thank you for help with error 404. I changed package to "war". But now when I run wildfly server Im getting error 403 (forbidden). I was looking how to fix it and still stay in the same place. It is so hard to get wildfly work with spring boot...
I updated files:
https://github.com/kuzyn007/LibraryCRUD
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.seweryn</groupId>
<artifactId>LibraryCRUD</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Spring: boot starter parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<!-- Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Starter for using Spring Data JPA with Hibernate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<!-- Starter for testing Spring Boot applications with libraries including
JUnit, Hamcrest and Mockito -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- The spring-boot-devtools module can be included in any project to
provide additional development-time features. -->
<!-- Applications that use spring-boot-devtools will automatically restart
whenever files on the classpath change. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- JSP Standard Tag Library -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.servlet.jsp</groupId>
<artifactId>jboss-jsp-api_2.2_spec</artifactId>
<version>1.0.2.Final</version>
</dependency>
<!-- … -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- … -->
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<finalName>LibraryCRUD</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
# MVC
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
# JNDI
spring.datasource.jndi-name=java:jboss/datasources/library
# JPA/HIBERNATE
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=pl.seweryn
spring.jpa.database=H2
spring.jpa.show-sql=true
Application.java
package pl.seweryn.init;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
//#Configuration
//#EnableAutoConfiguration
//#ComponentScan
#SpringBootApplication // same as #Configuration #EnableAutoConfiguration #ComponentScan - alternative
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Follow this instructions: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
You basically have to package your app as a WAR and implement SpringBootServletInitializer.
I found many mistakes in my project and now its working. I will write here answer for my app.
Physical naming was wrong. It should be: spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl. Or I could delete this line and it will be provided default from spring boot
I have Application.class in pl.seweryn.init package. I added #ComponentScan(pl.seweryn)
BookDaoImpl was not a spring component. Added line #Component

Maven, JUnit and Dagger - Dagger module could not be loaded

Note: I am building/testing via command line only.
I am running into an issue using Dagger, and have been able to reproduce the same issue in a very small test project. When trying to use Dagger in a unit test, I get the following error while running 'mvn clean test':
sanity(com.mycompany.app.AppTest): Module adapter for class com.mycompany.app.AppTest$TestModule could not be loaded. Please ensure that code generation was run for this module.
In the application, Dagger is compiling/building just fine, and injection is working great. The only issue is with the unit tests, and I feel that the unit tests arent picking up the results from the dagger-compiler, but am not sure how to test/fix this.
pom.xml:
<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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>com.squareup.dagger</groupId>
<artifactId>dagger</artifactId>
<version>1.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>1.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
AppTest.java
package com.audible.hushpuppy.dagger;
import org.junit.Before;
import org.junit.Test;
import dagger.Module;
import dagger.ObjectGraph;
import static org.junit.Assert.assertNull;
public class AppTest {
#Module(injects = AppTest.class)
public class TestModule{
}
#Before
public void setUp() throws Exception {
ObjectGraph.create(new TestModule());
}
#Test
public void sanity() throws Exception {
assertNull(null);
}
}
Aha - apparently annotation processing was completely turned off in an earlier commit, and I completely missed this line in the pom:
<compilerArgument>-proc:none</compilerArgument>

Resources