Java Maven-Spring Web Project - Separating Dependencies into Jar o War - java

I'm brand new in Maven. I'm working with Spring-Maven project, it generates a WAR file with 95Mb of dependencies (JARs) and 5Mb of my code.
There is anyway to separate code and libs? packaging my code into WAR and all dependencies into other WAR/Jar?.
Im deploying my app in tomcat server.
Thank you in advance!

You can exclude selected dependencies in the WAR by specifying scope as provided in your pom.
For example:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
Just make sure your dependencies are available in the classpath. You can put them in shared lib directory in server.
For example, in tomcat it's /lib directory.

Related

zookeeper jar in $HOME/.ivy2/jars for java application

My application is failing because of the following issue :
Source '/.../.ivy2/jars/org.apache.zookeeper_zookeeper-3.4.6.jar' does not exist
But I see, there exists a jar in that folder with "tests" suffix - org.apache.zookeeper_zookeeper-3.4.6-tests.jar
If I remove -tests from the name manually, the application runs fine.
I need to understand how can I place the jar my application is expecting in .ivy folder through my program/dependency.
Also, when are the all jars loaded in .ivy/jars folder, is it during the build?
I am using java with maven.
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<type>pom</type>
</dependency>
add this to your maven dependencies also the org.apache.zookeeper_zookeeper-3.4.6-tests.jar is for the test environment.

Maven dependency jar with prefix folder

My project pom.xml has a dependency to a 3rd party JAR file:
<dependencies>
<dependency>
<groupId>com.xyz.plugin</groupId>
<artifactId>abc</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
When I compile my project, maven fails to locate classes in this JAR file. This JAR file is structured as follows:
classes
|__com
|__xyz
|__plugin
|__<classes>
The compilation failed because this 3rd party JAR file has their classes com.xyz.plugin.* located inside the "classes" folder, instead of having the "com" folder at the top most level. Their MANIFEST file also does not include any classpath specified either.
Is there anyway I can write my pom.xml differently such that compilation will be able to locate the classes correctly within this 3rd party JAR? Thanks for any help!

Adding dependent project's properties file to the build target folder in Maven

I'm new to Maven, have looked for some help with my problem but couldn't find a proper solution.
I have a main project in Maven, which has a dependency to my second project:
<dependency>
<groupId>a.b.c</groupId>
<artifactId>childproject</artifactId>
<version>1.1</version>
</dependency>
Both projects have their properties files: mainproject.properties, childproject.properties.
When I deploy the main project, childproject.jar goes to \lib folder, along with all other dependencies. But the childproject.properties is "build into" the childproject.jar. If I place another childproject.properties file next to mainproject.properties in \configuration folder, it is seen and used.
What could I do so Maven places childproject.properties automatically in the \configuration folder?
childproject.properties
pom mainproject has :
<dependency>
<groupId>a.b.c</groupId>
<artifactId>childproject</artifactId>
<version>1.1</version>
</dependency>
And add file in :
mainproject
/src/main/resources/mainproject.properties
childproject , add file in:
childproject
/src/main/resources/childproject.properties
When you deploy the main project => childproject.jar contain in classpath childproject.properties
You do not need to create a directory 'configuration' for the project 'mainproject' uses 'childproject.properties'.

Include Maven Project into NON-MAVEN Dynamic Web Application

I've included the Maven based project into my application, added Deployment Assembly which shows that the .jar file gets copied to WEB-INF/lib/ImportedProject.jar.
Problem occurs when imported lib wants to reference the lib that it is using which, apparently, does not get included inside the ImportedProject.jar.
Example:
ImportedProject pom.xml has:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
When I add ImportedProject.jar to my Non-Maven project and run it, I get exception:
threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
My question is: How to add both that Maven project and its dependencies?
maven dependency plugin helps to get required project library with it's dependencies.
So, If you would like to add POM based project on NON-POM based project, all you would do is to execute mvn dependency:copy-dependencies command and you will have all dependencies on target/dependencies folder. Copy all jar from dependencies and paste to WEB-INF/lib directory.

Calling JSP files gives a NoSuchMethodError when app deployed outside of Eclipse

I have a maven project that works completely as intended inside of eclipse. It also builds and runs outside of eclipse, but when I try to call the frontend (JSP web pages) then I get the following:
Problem accessing /. Reason:
javax.servlet.ServletContext.getJspConfigDescriptor()Ljavax/servlet/descriptor/JspConfigDescriptor;
Caused by:
java.lang.NoSuchMethodError:
javax.servlet.ServletContext.getJspConfigDescriptor()Ljavax/servlet/descriptor/JspConfigDescriptor;
...
I've looked around and it seems that this message is associated with an incompatibility between Servlet 2.5 and Servlet 3.0. But I already have Servlet 3.0 as a dependency in my pom:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
So I can't figure out why this dependency that I need isn't being included when I build and deploy outside of eclipse even though the build itself is successful.
Any idea what could be causing this and how to fix it?
EDIT:
The JSP access isn't configured in web.xml. The index.jsp file is the welcome file set for the jetty server with this snippet:
// configure webapp
WebAppContext webroot = new WebAppContext();
webroot.setResourceBase("src/main/webapp");
webroot.setContextPath("/");
webroot.setWelcomeFiles(new String[] {"index.jsp"});
webroot.setParentLoaderPriority(true);
server.setHandler(webroot);
The remaining few jsp files are in the webapp folder.
EDIT 2:
I've examined the contents of the jar created when packaging my project and it appears that there are multiple copies of the javax/servlet/Servlet.class in the jar. This is a bit perplexing. I'm assuming that these other dependencies (listed below) that I have in my pom must be adding the additional Servlet.class files.
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.8.v20121106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.1.8.v20121106</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>8.1.8.v20121106</version>
</dependency>
But I'm not sure how to fix any of this...
If anyone has any ideas, I'd love to hear them. The only real constraints that I have is that I have to use jetty 8.1.8.v20121106.
It seems to me you might be deploying a servlet 3.0 on tomcat 6 which is not supported and would cause this exception.
An additional possibility is that your exporting additional .classes within your war that are interfering with tomcat's default libraries.

Categories

Resources