Using spring framework with maven instead of ant - java

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/

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.

Better way of using spring boot starters

I found two way of adding dependencies for spring boot rest service application.
Method 1 :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
Method 2:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Both method given above does the same job? Any difference in performance?
Thanks in advance
The difference between these methods is that spring-boot-starter-web contains more dependencies than just spring-boot-starter and spring-web.
For the version 1.5.8.RELEASE it will be:
spring-boot-starter
spring-boot-starter-tomcat
hibernate-validator
jackson-databind
spring-web
spring-webmvc
All other stuff depends on your requirements. For instance, if you're developing commercial product you must check all included transitive dependencies for their licenses.
The general advice is to use only features you need. Don't forget that you still can depend on top level artifact excluding not required parts using Maven feature.
At for the performance boost, basically it should not be that much. The difference is only if the Spring (with top-level artifact dependency) will load and auto configure some features which are not practically used in your code (during the classpath scanning). The startup time could be slightly increased by the same reason.
Hope it helps!
Obviously second approach is better i.e. using springboot starter pom.
Reasons I say that for are as follows:-
It allows zero configuration or auto configuration i.e. most of the web related settings would be given to you by default. e.g. by default tomcat server would be integrated, springboot dependencies would be added for you, automatic registration of converter and other web related dependencies etc. See this link.
You could take advantage of easy override i.e. if tomorrow you want to use jetty in place of tomcat just add the jetty dependency and it's configurations and now you could use jetty.
Your pom.xml would be neater and more readable as less no. of dependencies are put inside it and picked from starter poms.
Easy compatibility management. By default spring-boot picks up the version of starter parent. Hence, you could rest assured that all compatible dependencies would be downloaded as part of mvn dependencies and if you specifically want to upgrade any of those you could. But this gives advantage of upgrading to compatible dependencies by just changing the version of springboot starter parent(Note: You could use dependency management as well in place of starter parent pom. See this link).
Performance wise there would be a trade-off as springboot by default downloads more dependencies than minimum required initially. But over the time as application starts becoming mature most of these dependencies are used anyways.

Maven vanilla project for EJB2 and EJB3

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.

What are the Spring Hibernate Integration jars really required

I haven't yet found an answer to this question which is bugging me for long. I am trying to integrate Hibernate (3.6.7) with Spring (1.2.8). In order to do so i have to get a bunch of jars just to get it running. Isn't there a more cleaner of way of getting this done. The jars i am using rather forced to use are as below:
spring-1.2.8.jar
commons-logging-1.0.4.jar
hibernate-3.6.7.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
dom4j-1.6.1.jar
slf4j-api-1.6.1.jar
javassist-3.12.0.GA.jar
Is there no leaner way? The big list of dependent jars could potentially cause conflict during deployment to my appserver in the future. So its making me rethink about spring-hibernate integration. Is there a way to reduce this dependency list. My issue is not related to management of jars as maven is already being used, it more to do with the usage.
This answer was posted before the OP mentioned that he was using Maven. The question seems to be asking how he can use code without including it in his project. Given that there is no answer to that question I'll answer assuming he wants a better way of managing the dependencies that he needs.
The leaner way to do this is to use a dependency management tool such as maven. This allows you to define your project's dependencies in an xml file.
The dependencies you specify will also have dependencies and so on. These are transient dependencies and are very hard to manage without an automated tool.
This is also the best way to ensure that you only have the jars you require.
The dependencies you require are:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
and:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>

Eclipse giving out an error while adding springframework dependency in maven .pom

I created a Maven project in Eclipse using the webapp artifact and put the following lines in the .pom file upon reading an online tutorial.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
In the tutorial however, the version is 2.5.6. I replaced it with 3.0.5.RELEASE considering its the latest version.
But for this version eclipse is giving out an error
Missing artifact org.springframework:spring:jar:3.0.5.RELEASE:compile
What does this mean ? Can I add the required jar files to a lib folder and ask .pom file to take it from there as its being done in another tutorial on spring source website that uses Ant ?
Also Maven projects come with a different directory structure and seems to be doing much more than what Ant does in the spring source tutorial.
I am completely new to maven. in fact only a beginner in developing web applications using java. I did some tutorials from spring source and could deploy and run a spring mvc hello world app on apache tomcat. For this I used Ant and found it a great tool. But as I checked-out some example apps from spring source repo and it seems that Maven is more preferred and powerful than Ant. I am finding it a bit difficult to understand though.
Thanks
In version 3, Spring no longer provides the all-in-one Spring jar.
Note: The spring.jar artifact that
contained almost the entire framework
is no longer provided.
Source:
2.4 New module organization and build system
Depending on your project needs, use
<dependency>
<groupId>org.springframework</groupId>
<artifactId>${artifactId}</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
where ${artifactId} may be e.g.
spring-context (IOC core, standard ApplicationContext implementations)
spring-orm (ORM technologies: Hibernate, JPA, (I|My)Batis)
spring-webmvc (The Spring Web MVC framework)
spring-aop (Aspect-Oriented Programming support)
etc.
The selected dependencies will pull in their required libraries as transitive dependencies, so you usually just need the "most exotic technology". E.g. if you select Spring MVC and Spring ORM, you also get AOP, TX, Context, Web etc.
Reference:
Obtaining Spring 3 Artifacts with
Maven
In Spring 3.x they remove they removed this artifact. Now you have to declare dependencies on separate modules of Spring, such as
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
and so on.
Also see spring 3.0.5 library jars for the list of modules and description of dependencies between them.

Categories

Resources