Tomcat libraries to include in Eclipse with Maven - java

I'm building a web application with Eclipse using Maven. The server is going to be Apache Tomcat. Eclipse already has tomcat 6 libraries which you can include in your build path and Web Application Module facet to be chosen. That's the way I work without Maven.
However, Maven is able to include the required dependencies to use them in tomcat. My question is, what is the right thing, not to include them via Maven and continue doing that like before, or not to configure eclipse build path and make maven solve it?

The best approach for container specific API like the server API is include it in the maven POM however set the dependency as provided scope so it will be available in your class path for eclipse however maven will not package it in the WAR file when generating it. e.G.
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<scope>provided</scope>
</dependency>

As far as I know Maven do not solve it, if you do not specifically say that your project needs these dependences. The best way is to configure the pom.xml and set the dependencies and build the project using Maven and edit it in eclipse. So once you are done you can easily "package" (build the war) and deploy it in any server location.

Related

How to build the jar artifact for a Spring Boot project in IntelliJ IDEA

I'm using IntelliJ IDEA 2016.2.5 and I've used Spring Initializr to setup a Spring Boot project. Everything works fine and I can run and test my project in the IDE.
Right now, when I want to build the jar file, I run the following command from command prompt:
$ mvn package
where my pom.xml is located and the jar file is created perfectly (with a caveat that I'm going to tell you about it later). The thing is, I'm looking for a way to do the same from within the IDE.
So far I've tried to create a new jar artifact in Project Structure window (Alt+Ctrl+Shift+s) and include the project's output while for the maven dependencies I use Extract into Output Root. This way, the artifact can be built and run but there are lots of error messages given out and some of the functionalities are not working.
Does anyone know how to build a jar file for Spring Boot project from within IntelliJ IDEA?
The reason why I need to build my jar file in IDE is that my project is composed of several maven projects and I've imported them all as modules into one project. When I run the main module in the IDE, IntelliJ IDEA is smart enough to consider the module as the dependencies for the main one. But when I use the command prompt mvn, this is not happening (which is totally understandable since maven only knows of main project and not the rest). If only I can build from within the IDE..
To clarify my situation better, I've got a "Main" project and a "Library" which of course Library is a dependency of Main. Since I don't have maven repository (and at this point, I'm not looking for one), the only way I know of to make it possible for the maven to compile my code is to name Library in Main's pom.xml like this:
<dependency>
<groupId>org.example</groupId>
<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/lib/library-1.0-SNAPSHOT.jar</systemPath>
</dependency>
which as far as I know is highly discouraged. This is in the case that if I name the Library as an ordinary dependency:
<dependency>
<groupId>org.example</groupId>
<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
since a neighboring module (Library) outputs the exact same artifact, IntelliJ IDEA is capable of matching the dependency with that module and compile the code. But in case of mvn, since there's no module involved, this can not be done and the dependency is not satisfied. In other words, I can only config my environment to work with mvn or IntelliJ IDEA, but not both!
Go to the project root folder such as springbootdemo and run below commands:
mvnw clean package (This will create the target folder and generate the jar inside it)
mvnw spring-boot:run (This will run the spring boot application).
In my case application name is springbootdemo and its location is C:\Users\springbootdemo and I ran both commands at this location.
From within IDEA, you can go in the tool window "Maven Project", and here double click on Package.

Install library jar in pom maven

I have Java Maven Project with a folder lib with all jars that must be included in my project.
I don't know how I have to modify the POM to add all libraries. I want that Maven uses this libraries and I can use all in the project.
Lets be frank here: you are basically asking how to use Maven but then not use it at the same time. Maven is built around the fact that dependencies are managed from dependable repositories and then you come along and want to bypass that entire system by having local jars anyway as you would have in a project not managed through Maven - like one that is built with ANT.
The true clean solution to "not getting jars from a Maven repository" is to still get them from a repository - your own. Setup a local repository and put your third party dependencies there, then configure your Maven build to know about that local repository. If they are actually dependencies that exist in Maven central then you can setup your local repository to proxy them rather than manually installing them yourself.
http://maven.apache.org/repository-management.html
If you are using the release management features of Maven (or something like Hudson) then you should actually already have such a thing to stick your generated release artifacts into.
You have to know which libraries are you using. Imagine that you use in your project the joda-time library.
So you have to search your libraries (in this case joda-time) in maven repositories and add to your pom.xml like the following:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
Here the Maven repository --> Maven Repo

spring-webflow jars not available

I have wasted a 4 hours on finding a reason for not adding of webflow jars tp the reference library of my my modular maven project which is generated from appfuse.I want to add a webflow to this project. But when I do the registering as below, I get an error (I have still not run the project).
error is
Multiple annotations found at this line:
- Class 'org.springframework.webflow.mvc.servlet.FlowHandlerAdapter'
not found
But I have included the webflow dependencies in my pom in my web module project. But when I go the the reference library I can not find the webflow jars as well. I think this is the reason for this, But I can not wind a way to get the jars in to the reference library.
The generated pom is very lengthy. I will add it if it is needed.
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
Is there a compatibility issue with the spring versions. I am currently using 4.0.0.RELEASE I have changed it to 4.0.2.RELEASE as well. But no luck. I can not find a meaningful solution for this. I really appreciate some help on this.
Thanks
In Eclipse, right click the project and go to build path -> configure build path
From here, go to libraries and check that the Maven dependencies library is in there.
If its not click add library click maven managed dependencies, next, finish
Should add the libraries to the eclipse project

Dependency's Jar not being deployed to Web Project

I've got two Maven projects, A, that generates a jar, and B, that generates a war, in my Eclipse Workspace. A is a dependency of B, and, as such, is in B's POM.
My problem is that A is not being deployed to a .jar in /lib/ when I run a Tomcat server with B.
It seems strange A appears in B's Maven dependencies as a folder, not as jar. Is this anomalous in some manner? If so, how can I fix it?
If this is not the problem, perhaps it's with Tomcat. Do I need to configure it somehow to accommodate this dependency schema (in which the dependency appears as a folder)?
Thanks!
--
EDIT: After manually exporting the .war file of B, I've discovered that A is being put in /lib/ as an empty .ear file.
Here is a screenshot of the dependency been displayed as a folder:
Here are the relevant parts of the POM of A:
<groupId>br.com.reglare</groupId>
<artifactId>rule-model</artifactId>
<packaging>jar</packaging>
<version>1.8</version>
And B:
<dependency>
<groupId>br.com.reglare</groupId>
<artifactId>rule-model</artifactId>
<version>1.8</version>
</dependency>
#urir: Yes, it fails to find a class defined in A: http://990fca173882239e.paste.se/
The dependency should be defined in your pom in this format:
<dependency>
<groupId>xmlbeans</groupId>
<artifactId>xmlpublic</artifactId>
<version>2.2.0</version>
</dependency>
Also to view the dependecy tree use the 'dependency:tree' directive as follows:
mvn clean compile dependency:tree
If the dependecy is still not in the WEB-INF/lib folder of the war, then run the compile with the debugging flag and look for errors:
mvn -X clean compile
Assuming you are using m2e in eclipse to manage the webapp, you need to install m2e-wtp. You should also be able to find it within eclipse in the marketplace. It takes care of making sure that when you launch a maven web project through eclipse it gets all the dependencies in the right place. Once you install the plugin, you may want to delete your project and import it again as an "existing maven project" then the plugin will get it all setup correctly. Applying it after-the-fact has been iffy for me.

What is the connection between maven and my application?

I need to build java project. The project should include two modules: domain and web.
The domain module contains all the entities, the business logic and hibernate integration.
The web module should be depend on the domain module and contains web application using apache wicket.
I wonder about the maven usage.
Should I create a project and modules using maven? If so, how?
What kind of archetype are relevant for my project and modules?
What is better experience - create the project myself or use maven?
I am using intellij.
I'm assuming you don't need a server for others to access your code,
but rather, you want to use maven/ant for internal project
organization/dependency resolution/source organization.
Should I create a project and modules using maven?
Yes, either maven or ant will be useful for any non-trivial Java or Java EE project with external dependencies, and build/testing requirements.
If so, how?
Either ant/maven will allow you to easily setup a platform independent "build" file, so that you can easily resolve dependencies, build your jar executables, and run unit tests in order by issuing a single command, rather than multiple clicks to different plugins in whatver the ide-of-the-month is. You can do this in eclipse using the maven plugin to create a new maven project, or , as you suggest, by creating an artifact by running the regular mvn install.
What kind of archetype are relevant for my project and modules?
To learn use maven-archetype-quickstart.
For a regular (simple) j2ee app, try maven-archetype-webapp.
There is also a j2ee archetype as well.
What is better experience - create the project myself or use maven?
A simple, 3-step, robust method for setting up a maven project :
1) Use maven archetypes to create and setup your "hello world" project.
2) Import the maven project into your ide as a java project.
3) Edit/refine/fix code in your IDE, but use maven to build and test the whole application.
Update: external web frameworks
Creating a wicket (or gwt or any other framework) oriented web app will
Be best done following specific tutorials related to the framework itself. In order to add theframework libs, just paste the maven info in your pom.xml like thus, and run a "mvn install" command :
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>1.5.3</version>
</dependency>
I'd recommend you to use maven. The reasons why I use maven:
IDE agnostic. You can use idea, eclipse or some other ID.
Dependencies management
Powerful plugin system
You can manually create 3 maven module
app.parent with pom packaging and no parent.
app.domain with jar packaging and app.parent parent
app.web with war packaging and app.parent parent
and import app.parent to idea.
Also checkout Wicket quick-start Maven archetype creation page http://wicket.apache.org/start/quickstart.html

Categories