Maven vanilla project for EJB2 and EJB3 - java

I have a requirement where i have two develop two projects, one with EJB2.1 and one with EJB3.x.
The EJB2.1 will be an extension to an existing project hence the reason it is v2.1 and cannot be migrated to 3.x just yet.
Both projects have to be built using Maven and this is where i am struggling. Believe it or not, i have spent the last 3 days trying to find a simple example of a stateless EJB project built using Maven but have had no luck.
Here are a couple of pointers i found through googling but none of them work.
Create project using Eclipse
Eclipse allows you to create an EJB3 project. Latest versions dont allow you to create EJB2 projects. Also, you cant create a Maven EJB project.
Use the maven archetypes.
I have tried the following archetypes in Maven but none of them work
ejb2-j2ee13
ejb2-j2ee14
ejb-javaee6
ejb-jee5
ear-jee5
ejb-jee5
The above archetypes are only for EJB3 or for those that are for v2.x there will be some dependencies missing.
Jboss sample projects
I also found the following tutorials on the Jboss documentation http://anonsvn.jboss.org/repos/jbossas/projects/ejb3/trunk/docs/tutorial/reference21_30/. I intend to use the EJBs on Jboss v5.1. The projects on this URL are Maven projects but they also complain about missing dependencies (The ant build does work).
If anyone can point me to where i can download a vanilla "Hello world" maven project for both EJB2.x and EJB3.x (That would run on Jboss 5.1.x) i would really appreciate it.
I have spent so much time on google and have finally decided to ask here as i am not getting anywhere. I suspect that maybe they are making it difficult to find these dependencies as they dont want people to use EJB2.x anymore. The problem is some people have no other option.

you really dont need the archetype for Java EE5, or JEE6 there is so little boilerplate code, that all you need is a regular java archetype and add the dependency for
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
or
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>5.0</version>
<scope>provided</scope>
</dependency>
add your beans.xml and you are done. Avoiding any JBoss depdencies is actually a good practice, as this ensures your application remains portable.
Note: If you want to mock or use any classes from the respective javax.XXX packages, you will need to include your application server specific dependencies into your maven test scope. This is because Sun/Oracle altered the bytecode of the API jars, to guarantee nobody accidently uses these classes, instead of the ones provided by the server.

Related

LWM2M implementation on Spring boot java application

We have an application on java springboot which would interact with IoT devices via HTTP Rest API. However, there is an IoT device that communicates with the LWM2M protocol. So, I need to set up an LWM2M server and make the application an LWM2M client.
First I wanted to make a prototype on my local machine running application on Windows with eclipse ide. I tried importing the Leshan project from this link on eclipse workspace. However when maven clean install, it is not creating a jar file for every project. Attaching the result at eclipse console, when I do maven clean install..
My ask is:
Am I going the right way, in order to implement the LWM2M protocol locally?
How to resolve all jars not creating with Maven clean Install.
Our commercial LWM2M offering that is part of Cumulocity IoT in fact is a Spring Boot application which includes Leshan. So you are definitely on the right track.
While I am not able to disclose internals, I am happy to provide you some pointers how to get this flying.
In your pom.xml, declare the needed Leshan dependencies, for example:
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-core</artifactId>
<version>2.0.0-M9</version>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-server-core</artifactId>
<version>2.0.0-M9</version>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-server-cf</artifactId>
<version>2.0.0-M9</version>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-server-redis</artifactId>
<version>2.0.0-M9</version>
</dependency>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>californium-core</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>scandium</artifactId>
<version>3.7.0</version>
</dependency>
I assume you know how to set up a Spring Boot application using maven. If not, this tutorial shows precisely how it can be done.
In your spring boot application you then can construct a LeshanServer object and accept LWM2M traffic. Have a look at the leshan-server-demo maven module in the Eclipse Leshan source code on how to do that.
Questions about build issues get much better help and answers, if you use Eclipse/Leshan - github issues. Not all open-source projects are watching stackoverflow and so you can get a "first hand first class" answers only there.
I have skipped the integration testing by commenting its dependency in the pom file. Then all other modules got compiled.

Java Maven - How to resolve the version if two third parties depend on different versions of a library

I have a scenario as follows:
I am using maven as a build process. I am creating a web project in which I want to use a specific version of spring. This project also depends on a third party library which internally depends on different version of spring. I have a doubt that this will result two different versions of spring n class-path and unexpected behavior will be observed. I have few information which I wanted to get more clarification on.
Can I use maven BOM concept for this?
Can somebody explain with example how to achieve this?
Can somebody explain how do we make sure that third party wont behave abnormally if overall project depicts using a specific version using BOM?
If somebody can throw light on it and give a detailing reference, that would help me a lot.
Maven should know how to evict one or more of conflicting versions of an artifact.
However, you can influence that by simply excluding one of the transitively included dependency.
Example: the following code excludes the io.netty (transitive) dependency. In this way, you'd leave maven with the only other version as you decide/prefer.
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
<!-- The exclusion below makes sure that this specific version imported by hbase does not end up deployed -->
<exclusions>
<exclusion>
<artifactId>netty</artifactId>
<groupId>io.netty</groupId>
</exclusion>
</exclusions>
</dependency>
Regarding runtime behavior, you have to test and decide for yourself (that is if you aren't lucky to have your direct artifact that documents versions of its own dependency)
You can use the concept of BOM but this won't avoid the conflicting issue of libraries by itself. It's very common that projects have one or more library which depends on the same other with different versions. In this case, when you want to force some specific library version for that third party library you must explicit it in your POM by using < exclusion > markups. This is not an easy task, once that projects usually have many libraries. So you need a tool to provide you an easy way to visualize a dependency hierarchy of your project libraries. There are some IDE plugins for this. Some versions of Eclipse, for example, have the maven plugin included in it, which provide a Dependency Hierarchy view ( a kind of dashborad of libraries and their dependencies ). Once you detected a library which should not using other library dependency ( wrong version for example ), you go at the this dependency in the pom and use the exclusion markup adjust the dependency version. Using the tool will make this task very simple.

Eclipse Warnings: class javax.persistence.* not found

This is my first time really playing around with Java development using Eclipse. I am trying to use EclipseLink's implementation of the JPA. I moved all of my entities into a separate package "entities". I have the persistence.xml in a separate JPA project called "dataModeling".
Everything builds and runs fine.
Just about every project depends on my entities. However, I'm seeing a warning Class javax.persistence.Entity not found - continuing with a stub., etc. showing up because the dependent projects don't reference EclipseLink.
The solution is to go into each dependent project's properties and under Java Build Path > Libraries, click Add Library, then User Library and then select EclipseLink.
However, to me, it doesn't make sense to reference EclipseLink in every project! That's an implementation detail I don't want to burden other projects with. It looks like this is happening because the other projects see the annotations and don't recognize them.
So the real question is: how can I use JPA (via annotations) without every other project needing to reference my JPA implementation?
Your pom.xml should contain:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
<scope>compile</scope>
</dependency>
the first one is Eclipse-Link (which you already have), the second one is Persistence API which is lacking.
If you are not using maven - make sure that javax.persistence-2.0.0.jar is on your classpath.
Note that this is version 2.0.0, the newest is 2.1.0
update
The project which makes use of EntityManager should have these dependences. Putting entities and persistence.xml in separate jar file still requires the other project that uses it to fulfill above dependencies.
Thanks to #neil-stockton and #chris, I was able to figure out what was going on. Most JPA implementations have a copy of the javax.persistence JAR floating around somewhere. Most of them are bundled with everything else, leading to my dependency nightmare. There doesn't appear to be a de facto implementation floating around.
In my case, I used the copy that showed up under my Eclipse plugins directory. These annotations were truly empty in that did not have any unwanted dependencies. The JAR file (javax.persistence._<version>.jar) only showed up after I added the Dali and EclipseLink plugins (one or the other).

How to add Maven dependencies to an Android project, in Eclipse, for someone with zero Maven experience?

I've made a few Android apps, but every time I need a library, I'd either download the jar and include it in my /libs folder, or clone the repository and include it as an Android Library. However, many of the more robust libraries recommend using Maven, and considering that more and more people are using it, AND Gradle apparently uses it as well (another system I need to eventually adapt to), I feel like it's time I finally get on board. Unfortunately, most of the tutorials and questions regarding Maven that I've found seem to assume at least a basic working knowledge of the system. What I need is a "Baby's First Maven Tutorial," so to speak. Can anyone help?
For example, the networking library Ion. The Maven dependency block for it is as follows:
<dependency>
<groupId>com.koushikdutta.ion</groupId>
<artifactId>ion</artifactId>
<version>1.2.4</version>
</dependency>
How would one incorporate this library into an existing Android project in Eclipse, via Maven, starting from the very beginning?

Using spring framework with maven instead of ant

Is there a tutorial somewhere which shows how to use spring framework with maven instead of ant? This one seems very good but it's all built with ant.
EDIT
I really don't know which answer to accept both are valid. I'll wait for some time let the community decide
Basically, the build.xml of the tutorial has 3 main targets :
build the application
deploy it on Tomcat server
Unit testing using a in-memory database (hsqldb)
Regarding the first point, you will just need to create a war project on Maven. As you told in your comment, you are already using Maven in anothers projects, so I don't think it will cause you lots of troubles. You will just need to add the Spring dependency:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.2</version>
</dependency>
The second part concerns the deployment on Tomcat. Just use the cargo plugin for that.
For the last point, you will just need to add the HSQLDB dependency in your pom.xml:
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
<scope>test</scope>
</dependency>
Then, you will have to instanciate the database in one of your JUnit test case...
If you already know maven, then you can quickly start working with spring using this archetype
appfuse-basic-spring
Note that it sets everything up for Spring MVC, Spring and Hibernate so you should remove unnecessary files. Still, it's a great start.
If you don't know much about maven templates check this URL that explains how to use archetypes. An archetype is basically a project template.
The complete list of templates can be found here.
The Spring 3.0 samples are Maven based:
https://src.springframework.org/svn/spring-samples/mvc-basic/trunk/
https://src.springframework.org/svn/spring-samples/petclinic/trunk/
https://src.springframework.org/svn/spring-samples/spring-travel/trunk/

Categories

Resources