How to turn my Jersey REST API into an executable JAR? - rest

I am using Jersey, Maven; and could use Jetty, Tomcat or J2EE Preview (is that embeddable?).
What is the easiest way to port my REST API as a standalone/executable JAR?
Can I do it without Spring Boot?

Follow these steps to create a standalone application with Jersey and Tomcat:
Adding Maven dependencies
Add the following dependencies and properties to your pom.xml:
<properties>
<tomcat.version>8.5.23</tomcat.version>
<jersey.version>2.26</jersey.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>${tomcat.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
Creating JAX-RS resource classes
Define your JAX-RS resource class(es). The following is just an example:
#Path("hello")
public class HelloWorldResource {
#GET
#Produces(MediaType.TEXT_PLAIN)
public Response helloWorld() {
return Response.ok("Hello World").build();
}
}
Creating a Jersey configuration class
Create a class to configure your Jersey application:
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
packages("com.example");
}
}
Creating a launcher class for Tomcat
Create a class to launch Tomcat and deployment your application:
public class Launcher {
private static final String JERSEY_SERVLET_NAME = "jersey-container-servlet";
public static void main(String[] args) throws Exception {
new Launcher().start();
}
void start() throws Exception {
String port = System.getenv("PORT");
if (port == null || port.isEmpty()) {
port = "8080";
}
String contextPath = "";
String appBase = ".";
Tomcat tomcat = new Tomcat();
tomcat.setPort(Integer.valueOf(port));
tomcat.getHost().setAppBase(appBase);
Context context = tomcat.addContext(contextPath, appBase);
Tomcat.addServlet(context, JERSEY_SERVLET_NAME,
new ServletContainer(new JerseyConfiguration()));
context.addServletMappingDecoded("/api/*", JERSEY_SERVLET_NAME);
tomcat.start();
tomcat.getServer().await();
}
}
Adding Maven plugin for creating an executable JAR
Finally add the Maven Shade plugin to create an executable JAR, where the mainClass attribute references the launch class:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<finalName>tomcat-embedded-example-${project.version}</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Launcher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Compiling and running the application
To compile and run the application, follow these steps:
Open a command line window or terminal.
Navigate to the root directory of the project, where the pom.xml resides.
Compile the project: mvn clean compile.
Package the application: mvn package.
Look in the target directory. You should see a file with the following or a similar name: tomcat-embedded-example-1.0-SNAPSHOT.jar.
Change into the target directory.
Execute the JAR: java -jar tomcat-embedded-example-1.0-SNAPSHOT.jar.
The application should be available at http://localhost:8080/api/hello.
See more
How to deploy a JAX-RS application on a Java SE environment?

Follow these steps to create a standalone application with Jersey and Jetty:
Adding Maven dependencies
Add the following dependencies and properties to your pom.xml:
<properties>
<jetty.version>9.4.7.v20170914</jetty.version>
<jersey.version>2.26</jersey.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
</dependencies>
Creating JAX-RS resource classes
Define your JAX-RS resource class(es). The following is just an example:
#Path("hello")
public class HelloWorldResource {
#GET
#Produces(MediaType.TEXT_PLAIN)
public Response helloWorld() {
return Response.ok("Hello World").build();
}
}
Creating a Jersey configuration class
Create a class to configure your Jersey application:
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
packages("com.example");
}
}
Creating a launcher class for Jetty
Create a class to launch Jetty and deployment your application:
public class Launcher {
private static final String JERSEY_SERVLET_NAME = "jersey-container-servlet";
public static void main(String[] args) throws Exception {
new Launcher().start();
}
void start() throws Exception {
String port = System.getenv("PORT");
if (port == null || port.isEmpty()) {
port = "8080";
}
Server server = new Server(Integer.valueOf(port));
ServletContextHandler context = new ServletContextHandler(server, "/");
ServletHolder servlet = new ServletHolder(JERSEY_SERVLET_NAME,
new ServletContainer(new JerseyConfiguration()));
context.addServlet(servlet, "/api/*");
try {
server.start();
server.join();
} finally {
server.destroy();
}
}
}
Adding Maven plugin for creating an executable JAR
Finally add the Maven Shade plugin to create an executable JAR, where the mainClass attribute references the launch class:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<finalName>jetty-embedded-example-${project.version}</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.Launcher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Compiling and running the application
To compile and run the application, follow these steps:
Open a command line window or terminal.
Navigate to the root directory of the project, where the pom.xml resides.
Compile the project: mvn clean compile.
Package the application: mvn package.
Look in the target directory. You should see a file with the following or a similar name: jetty-embedded-example-1.0-SNAPSHOT.jar.
Change into the target directory.
Execute the JAR: java -jar jetty-embedded-example-1.0-SNAPSHOT.jar.
The application should be available at http://localhost:8080/api/hello.
See more
How to deploy a JAX-RS application on a Java SE environment?

Related

How to create a Scala executable jar file that is built with Maven, and has log4j included, using IntelliJ IDEA IDE [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Longtime Java developer trying to learn Scala(not with Spark). I wanted to build a basic Scala program that can be built using Maven and run as an executable jar. In addition, I wanted to go ahead and incorporate Log4j into it, so that I have a basic framework for building stand-alone jars. I had some trouble googling all the pieces I needed to do this, but I finally figured this out, so I wanted to post how to do this for others to use.
So, it turns out that I had to go with Log4j2(which I have never used) instead of Log4j(which I have used a lot). So, the basic environment I am using here is:
OS = Windows 10
IDE = IntelliJ IDEA 2018.1.6 (Community Edition)
Java SDK = 1.8.0_111
Language = Scala 2.12.6
Build Tool = Maven
Steps:
1. Go to File -> new project
Select a Maven project, and the net.alchim31.maven:scala-archetype-simple archetype:
starting a new maven project
Fill out group ID, artifact ID, and version on the next page:
groupId, artifactId, and version
Then go with the defaults for the next 2 pages, and click "Finish"
2.) Import Maven dependencies:
When the project first opens up, you should see a small popup in the bottom right corner, click on “Enable Auto-Import”:
auto-import
3.) make a resources folder inside your project's main/scala folder
4.) go to File -> Project Structure:
In this window, you want to add this src/main/resources path as a resources folder,
so in the project structure window, click modules, then resources, then rick click on
your resources folder and select “resources”.
Then you should see your folder on the right in purple as a resources folder, click “Apply”, then click “OK”:
Project Structure window
5.) make a new package called "classes" inside your src/main/scala folder
6.) In the src/main/resources folder, make a new configuration file called log4j2.properties. This is for the log4j configuration.
7.) In this file, put the following information in it to configure a rolling log file called msggen.log that roles over every 10MB:
name = PropertiesConfig
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = /home/ubuntu/logs/msggen/msggen.log
appender.rolling.filePattern = /home/ubuntu/logs/msggen/msggen.%d{dd-MMM}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
logger.rolling.name = msgGenLog
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
8.) In your pom.xml, you need to add the log4j2 dependencies:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api-scala_2.12</artifactId>
<version>11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.0</version>
</dependency>
9.) You also want to add build plugins for when you want to generate the executable jar.
Inside the build and plugins tags, add the following plugin tags(change the finalName tag to what you want your jar name to be):
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>msggen</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.scala.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
10.) In your src/main/scala/classes folder that you made earlier, make a Scala object called LogUtil.scala, with the following code:
package classes
import org.apache.logging.log4j._
object LogUtil {
val msggenLogger:Logger = LogManager.getLogger("msgGenLog");
def msggenLoggerDEBUG(message: String): Unit = {
this.msggenLogger.debug(message)
}
}
11.) change your App.scala object to invoke your logger, here’s my code:
package com.scala
import classes.LogUtil
/**
* #author ${user.name}
*/
object App {
def main(args : Array[String]) {
LogUtil.msggenLoggerDEBUG("Hi there!");
}
}
12.) right click on the App.scala object and select “Run ‘App’” to test that the log file gets made and populated(set the config property appender.rolling.fileName to set where you want the log file to be created):
test log message
To Build the JAR file:
1.) Go to File -> project structure again, and select the “Artifacts” tag on the left, then click the “+” button, then JAR -> from modules with dependencies:
project structure for artifact
2.) In the “create JAR from modules” window, select your App.scala class as the main class, and make sure to specify the src/main/resources folder to where you want your manifest file to be made in(change it from src/main/scala):
create jar from modules window
3.) Click OK and make a note of the "Output Directory" field which tells you where your jar is going to be after it is made.
4.) Click “Apply” and then “OK”.
5.) In the main window, go to “Build” -> Build Artifacts -> then in the little window that pops up, select “build” again
6.) Now in the Output Directory path noted earlier, you should see your jar file.
7.) I’m on Windows, so open a command line and go to the location where your jar is, and run it by typing
Java -jar msggen.jar
8.) go to your log file again to verify that your test message was logged:
log file with 2nd message
So, now you should have a basic framework for a Scala application with Maven and Log4j2 integration within it.
Here is my full pom.xml for reference:
<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>com.scala</groupId>
<artifactId>msggen</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}</name>
<description>My wonderfull scala app</description>
<inceptionYear>2018</inceptionYear>
<licenses>
<license>
<name>My License</name>
<url>http://....</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.12.6</scala.version>
<scala.compat.version>2.12</scala.compat.version>
<spec2.version>4.2.0</spec2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api-scala_2.12</artifactId>
<version>11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.0</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.compat.version}</artifactId>
<version>3.0.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-core_${scala.compat.version}</artifactId>
<version>${spec2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-junit_${scala.compat.version}</artifactId>
<version>${spec2.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.3.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<!-- Tests will be run with scalatest-maven-plugin instead -->
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>2.0.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>TestSuiteReport.txt</filereports>
<!-- Comma separated list of JUnit test class names to execute -->
<jUnitClasses>samples.AppTest</jUnitClasses>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Eclipse oxygen Release 1a + Java 9 + maven project is not working

I tried java 9 with eclipse oxygen 1a Release. As far as Simple java 9 project is concern. It is working. Here is the screen shot of java 9 simple program
Here is my Welcome.java. It can be seen that I am using Java 9 feature in this program
package com.jdojo.intro;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to the Module System.");
// Print the module name of the Welcome class
Class<Welcome> cls = Welcome.class;
Module mod = cls.getModule();
String moduleName = mod.getName();
System.out.format("Module Name: %s%n", moduleName);
}
}
But with Maven project with Java 9 I am having problem. I created almost same project with maven. Here is my pom.xml file. I also used junit 5 dependency
<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>pk.training.basit</groupId>
<artifactId>Ch3-02-HelloWorldMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Ch3-02-HelloWorldMaven</name>
<url>http://maven.apache.org</url>
<properties>
<java-version>9</java-version>
<!-- Unit testing -->
<junit.version>4.12</junit.version>
<junit-jupiter-engine.version>5.0.1</junit-jupiter-engine.version>
<junit-platform-runner.version>1.0.1</junit-platform-runner.version>
<!-- Logging -->
<log4j.version>2.9.1</log4j.version>
<jboss-logging.version>3.3.1.Final</jboss-logging.version>
<junit-jupiter-engine.version>5.0.1</junit-jupiter-engine.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<maven-failsafe-plugin.version>2.20</maven-failsafe-plugin.version>
<maven-javadoc-plugin.version>2.10.4</maven-javadoc-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Unit Testing -->
<!-- JUnit Jupiter test engine implementation, only required at runtime. -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter-engine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit-platform-runner.version}</version>
<scope>test</scope>
</dependency>
<!-- Logging-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
......
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>enforce</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>pk.training.basit.HelloFXApp</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</plugin>
</plugins>
</build>
The project structure can be seen in the screen shot. Here is my AppTest.java
package pk.training.basit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class AppTest {
#Test
void myFirstTest() {
assertEquals(2, 1 + 1);
}
}
If module-info.java file is not present in src/main/java. Then this test case run. By simply mouse-right click and select Run As--> Junit Test. If I select the java file, which is Welcome.java and select Run As --> Java Application. Then I get the following output
Welcome to the Module System.
Module Name: null
Which is OK. Because now it is considering class path. And no module is present on module path. So module name is null. But as soon as I introduce module-info.java in src/main/java/ folder. Things start getting messy. Here what I tried with module-info.java. First I tried this
module pk.training.basit {
}
Same as my First project without maven. I get error in my AppTest.java. Imports are not resolving. Now if I right click on my Welcome.java and select Run As --> Java Application . It says error exist want to proceed. If I select yes. I get the following output
Error occurred during initialization of boot layer
java.lang.module.FindException: Module pk.training.basit not found
Now if I delete my module-info.java. And then right mouse click on my project name which is Ch3-02-HelloWorldMaven and select Configure --> Create module-info.java. It asks me for module name. If I enter my module name pk.training.basit and click Finish. Then here is the result
Now if i again try to run the Welcome.java. I get the error
Error occurred during initialization of boot layer
java.lang.module.FindException: Module pk.training.basit not found
Now if i remove my AppTest.java. And change module-info.java to this
module pk.training.basit {
exports pk.training.basit;
//requires org.junit.jupiter.api;
}
And try to run the Welcome.java. Although there is no error present now in project. I get the output
Error occurred during initialization of boot layer
java.lang.module.FindException: Module pk.training.basit not found
I don't know what I am doing wrong. Or there is problem with maven or eclipse. But here what I tried with eclipse oxygen 1a, Maven and eclipse.
Also I just have java 9 GA. No java 8 or java 7 on my machine. For just java 9 I have to include this in eclipse.ini file. -vm C:\Program Files\Java\jdk-9\bin\javaw.exe
....
openFile
--launcher.appendVmargs
-vm
C:\Program Files\Java\jdk-9\bin\javaw.exe
-vmargs
-Dosgi.requiredJavaVersion=1.8
-Dosgi.instance.area.default=#user.home/eclipse-workspace
-XX:+UseG1GC
....
If I am doing something wrong. Then please let me know.
Also you can't have more than one module-info.java file in one project in eclipse. Like if we consider this link Java 9 multi module project. Then for multi module project the structure is you have project root folder. Then in project root folder you have one source folder for one module. And another source folder for another module. Both source folders have their own module-info.java file.
But if I try to do the same in eclipse. Then I get the error. Multiple modules-info.java files found on class path.
Like the idea is you have one maven project. Which manage all your dependencies. Like spring, apache, etc. And in this project you have one source folder like module.web which contains your web modules related java files and its own module-info.java. You have another source folder module.service. which contains its own module-info.java.
But it seems you can't do it in eclipse in single project. I don't know it's valid or not but I tried it and I see you can't do it in eclipse in this way.
Anyways if I am doing something wrong then please let me know.
Thank you.

JBoss error: org.jboss.as.controller.management-operation with mysql-connector-java-5.1.39-bin.jar

I'm creating a barebones project from an example a tutor gave me to run on WildFly. Whenever I run the server I get the error:
[org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("add") failed - address: ([("deployment" => "mysql-connector-java-5.1.39-bin.jar")]) - failure description: "WFLYSRV0137: No deployment content with hash 3ab47e0f358f83fa29c2b3ba8106c0cc016e1198 is available in the deployment content repository for deployment 'mysql-connector-java-5.1.39-bin.jar'. This is a fatal boot error. To correct the problem, either restart with the --admin-only switch set and use the CLI to install the missing content or remove it from the configuration, or remove the deployment from the xml configuration file and restart."
My pom.xml looks like this:
<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>BrassDucks</groupId>
<artifactId>BrassDucks</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<description>Brass Ducks</description>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1-m01</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.ejb/ejb-api -->
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
And while I don't think these are relevant my ApplicationConfig.java:
package brass.ducks.application;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/")
public class ApplicationConfig extends Application {}
and my Controller.java looks like this:
package brass.ducks.controller;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
#Path("/")
#Produces(MediaType.APPLICATION_JSON)
public class Controller {
private final static Logger log = LoggerFactory.getLogger(Controller.class);
#GET
#Path("hello")
public String listStaff() {
return "hello";
}
}
How do I remedy this error?
I believe are getting some sort of version conflict on the mysql driver.
Ideally, you should install and use the correct version of the mysql driver as a module. You can install it using the maven plugin or create a module manually:
http://www.mastertheboss.com/jboss-frameworks/maven-tutorials/jboss-maven/configuring-maven-wildfly-plugin
https://developer.jboss.org/thread/271155
https://docs.jboss.org/wildfly/plugins/maven/latest/examples/add-resource-example.html
https://javavolker.wordpress.com/2014/03/14/automated-jdbc-driver-and-datasource-deployment-with-wildfly-8/
Once the version of the mysql driver in your pom.xml and the one configured in wildfly are consistent, your error should go away.

Jersey + JPA deployment on Glassfish not working without restart

For a webapp project, I am using Eclipse + Jax-RS Jersey 2.5 + JPA 2.0, and I am facing some weird problems after deploying the application on Glassfish server from Eclipse.
In fact, everything works fine if I hit "Run on server", let the deployment end and then restart the Glassfish server ; btw, it's not very effective when developing, 'cause it leads to a lot a wasted time...
If I just hit "Run on server", then none of my EJB's are being injected, resulting in NullPointerException that prevent me from doing anything.
I used Maven for dependencies management, could it come from it ? Should I use another way than "Run on server" to redeploy my application after code change ?
Thanks
EDIT : Here is the architecture of the project :
src/
main/
java/
/* Application packages */
resources/
META-INF/
persistence.xml
webapp/
WEB(INF/
beans.xml
/* App templates */
pom.xml
EDIT 2 :
Even with this script it does not work :
/opt/apache-maven-3.1.1/bin/mvn clean package
/opt/glassfish4/glassfish/bin/asadmin --host localhost --port 4848 --user admin --passwordfile /opt/glassfish4/glassfish/domains/domain1/config/admin-keyfile --interactive=false --echo=true --terse=false undeploy --keepreposdir=true --isredeploy=false --cascade=false --_ignorecascade=false --_classicstyle=false configurator
/opt/glassfish4/glassfish/bin/asadmin --host localhost --port 4848 --user admin --passwordfile /opt/glassfish4/glassfish/domains/domain1/config/admin-keyfile --interactive=false --echo=true --terse=false deploy --name configurator --force=false --precompilejsp=false --verify=false --generatermistubs=false --availabilityenabled=false --asyncreplication=true --keepreposdir=true --keepfailedstubs=false --isredeploy=false --logreportederrors=true --_classicstyle=false --upload=false /home/jeremy/workspace-configurator-v2/trunk/configurator/target/configurator.war
EJB are injected with EJB annotation, and specifying the LOOKUP name :
#EJB( name=CustomApplicationService.LOOKUP_NAME )
private CustomApplicationService customApplicationService;
Class :
#Stateless
public class CustomApplicationService {
public final static String LOOKUP_NAME = "java:global/configurator/CustomApplicationService";
#PersistenceContext( name=UtilConsts.DATABASE_POOL )
private EntityManager em;
/**** */
}
Finally, 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>com.virtualsensitive</groupId>
<artifactId>configurator</artifactId>
<packaging>war</packaging>
<version>2</version>
<name>configurator</name>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.ejb</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>
<build>
<finalName>configurator</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<jersey.version>2.5.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

Deploy a JAR file in Cloudbees - 502 Bad Gateway

I am trying to deploy the simplest application on Cloudbees, using the Spark Java framework. This produces a Jar file that I tried to deploy through the Jenkins push->deploy, but it warns me that the deploy plugin cannot deploy a jar file...
Whatever, I deployed my jar through the CloudBees SDK and the CLI :
bees app:deploy -t java -R java_version=1.7 target\myapp-with-dependencies.jar
And then it tells me that the application has been deployed to my URL. But when I try to reach this URL, I get a 502 Bad Gateway Error...
However, whether if run my main class through IntelliJ or with the Jar file produced by maven, the URL 127.0.0.1:8080 returns me the expected Hello Spark.
Here is my main class :
public class HelloSpark {
public static void main(String[] args) {
String port = System.getProperty("app.port","8080");
//So that the port is the one used by CloudBees
Spark.setPort(Integer.parseInt(port));
Spark.get(new Route("/") {
#Override
public Object handle(Request request, Response response) {
return "Hello Spark";
}
});
}
}
And here is my pom file :
<?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>spark-from-scratch</groupId>
<artifactId>spark-from-scratch</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>spark</groupId>
<artifactId>spark</artifactId>
<version>0.9.9.4-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>HelloSpark</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I think you need to specify your main class in this way:
bees app:deploy -t java -R class=your.main.Class -R java_version=1.7 PATH_TO_APP_PACKAGE
I think it's necessary to specify it in your command, you can read more here:
https://developer.cloudbees.com/bin/view/RUN/Java+Container