i am trying to compile my OSGi bundle against OSGi specification 4.3 using OpenJDK7 but i am getting error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5:compile (default-compile) on project example: Compilation failure
[ERROR] /tmp/baka/example/src/main/java/org/example/Activator.java:[14,24] error: type ServiceReference does not take parameters
here is my Activator.java:
package org.example;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
public class Activator implements BundleActivator {
#Override
public void start(BundleContext bundleContext) throws Exception {
ServiceReference<Runnable> ref = bundleContext.getServiceReference(Runnable.class);
}
#Override
public void stop(BundleContext bundleContext) throws Exception {
}
}
and my 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>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>example</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Private-Package>org.example</Private-Package>
<Bundle-Activator>org.example.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
This error is not appears when i am using OpenJDK 6. Any hints how to get it working with OpenJDK 7?
You need to recompile the OSGi source code with javac from Java 7. OSGi compiled the code with Java 6 javac using -target jsr14. Java 7 javac removed support for compiling against such class files: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7078419
Starting with R5, OSGi will no longer ship -target jsr14 class files.
[Updated 31 Oct 2012]
OSGi has now provided recompiled 4.3 jar files for Java 7. See http://blog.osgi.org/2012/10/43-companion-code-for-java-7.html
Related
As a green hand, I created a HelloWorld servlet:
package com.rx.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/helloworld")
public class HelloWorld extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter writer = response.getWriter();
writer.write("<html><body>Hello World</body</html>");
}
}
Then there is the following configuration in my 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>rx.helloworld</groupId>
<artifactId>simpleservlet</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>simpleservlet Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<war.name>simpleservlet</war.name>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<servlet.version>4.0.1</servlet.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${war.name}</finalName><!-- name of the bundled project when it is finally built -->
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.9.v20160517</version>
<configuration>
<httpConnector>
<!--host>localhost</host-->
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>
</project>
I read from servlet specification document, where told that
A web application is NOT required to contain a web.xml if it does NOT contain any
Servlet, Filter, or Listener components or is using annotations to declare the same. In
other words an application containing only static files or JSP pages does not require
a web.xml to be present.
I indeed used the #WebServlet, so did not include the web.xml.
The full code base is here
Question: But when I try to run it with jetty with command mvn jetty:run, I can't find my servlet at all. How to resolve this problem?
Eventually figured out by myself, say it missed a configuration in the maven-war-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
The failOnMissingWebXml should be configured false, which is not the default value
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.
I am learning Spring Boot. I have just created my first project using maven, Spring Boot, Spring Rest support and MongoDB. It compiles successfully, but it resolves all the dependencies, but do not compile the java classes at all.
After compilation, jar file is correctly created, it contain lib folder, metadata etc, but it do not contain project class file at all.
Hence when i run the project with mvn spring-boot:run, it throws an exception that class not found (Main method class for Spring boot initialization).
Please suggest, what I am doing wrong here, here is my maven configuration class:
<?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>
<properties>
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.assignment.BootInitializer</start-class>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.9.RELEASE</version>
</parent>
<groupId>com.assignment</groupId>
<artifactId>spring-assignment</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
And here is the main initializer class:
package com.assignment;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#EnableAutoConfiguration
#ComponentScan
public class BootInitializer {
public static void main(String[] args) {
SpringApplication.run(BootInitializer.class, args);
}
}
What I need to do, to ensure that maven is compiling the java classes and including them in the jar file.
Thanks.
I am learning Spring Boot.
Okay, good. Let's do one step at a time. Go to start.spring.io and generate a template project with whatever dependencies you want and whatever build tool (maven / gradle) you like. Build it and run it and see whether it is coming up or not. Then incrementally build on top of it.
I'm trying to run my Spring Boot application on a local Tomcat 8 server, but can't get it to run. It works fine in Eclipse and mvn spring-boot:run.
I added the SpringBootServletInitializer and changed the pom.xml as recommended. But it seems that it never runs.
Here is the main class:
#SpringBootApplication
public class SasuApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SasuApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SasuApplication.class, args);
}
}
And here is my pom.xml
<?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>net.sverin.poc</groupId>
<artifactId>sasu</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>sasu</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>sasu</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
What am I missing?
Old question I know, but I just had this problem with Spring Boot 2.0.3 and Tomcat 8.5. My solution was to remove the <absolute-ordering> element from my web.xml file.
Another reason could be Tomcat version.
Spring boot does not work with Tomcat 10 #22414
Perhaps the problem is in the Java version and environment variables...
check JRE_HOME, after I set to 1.8 it started working
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>