Add local lib in pom.xml but not found when running war file - maven

My develop env is eclipse STS.
I am stuck in this issue as follow:
First add local jar file in pom.xml
<dependency>
<groupId>testjdbc</groupId>
<artifactId>test-jdbc</artifactId>
<version>8.4.1.2032</version>
<scope>system</scope>
<systemPath>${project.basedir}/patch/testjdbc.jar</systemPath>
</dependency>
It is ok in develop environment not compile error and I check this file also in pakcge file after packaged this project.
But it is not found when running in STS Server.
I have checked "Browse Deployment Location" and testjdbc.jar does not existed.

This works as designed. System scope dependencies will not be packed into your application.
provided This is much like compile, but indicates you expect the JDK
or a container to provide the dependency at runtime. For example, when
building a web application for the Java Enterprise Edition, you would
set the dependency on the Servlet API and related Java EE APIs to
scope provided because the web container provides those classes. This
scope is only available on the compilation and test classpath, and is
not transitive.
system This scope is similar to provided except that you have to
provide the JAR which contains it explicitly. The artifact is always
available and is not looked up in a repository.
See Maven Dependency Scope

Related

spring boot startup fails when using war built on jenkins

We have a spring-boot application, built as a .war assembly (Maven build), which is deployed on the server with simple java -jar command.
As a part of CI/CD, the same code is being built on Jenkins. The Jenkins server is CentOS and the target deployment server is RedHat. The .war file built on Jenkins fails with Spring Beans Cyclic Dependency resolution error, while the same code when built on local(Mac OS) or on RedHat server works fine.
I tried to look through the war file, for each and every dependency, and everything looks same except for jsp-api-2.1 jar. The jsp-api-2.1.jar included in .war built on Jenkins has Created-By: 1.5.0_06-b04 in MANIFEST.MF, while the jsp-api-2.1.jar included in .war built on local (MacOs) or on Redhat has Created-By: 1.5.0_04-b05 in MANIFEST.MF. Other than this all other dependencies are the same.
The war files' sizes differ slightly, however, I could not find any difference other than what mentioned above.
The error which we are getting on Jenkins built jar is below,
Description:
The dependencies of some of the beans in the application context form a cycle:
| Class A
↑ ↓
| Class B
└─────┘
Does Maven generate any OS specific files, or are the dependencies downloaded from repository OS specific?
I can't really think of anything as both the .war files look similar. Any information on this will be helpful.

Why third party dependency is required exclusively from OSGi container even if I have it in my maven dependencies?

I want to know why OSGi do not respect the maven dependenceis.
I want to create one app in OSGi(AEM). I want to communicate(CRUD) to the database with the help of JPA(eclipselink).
I created maven project with aem-archetype.
Added all required dependencies(of JPA) into my maven project's pom file.
No errors in Eclipse, I built the project via mvn clean install and installed it into AEM(CQ5) via mvn sling:install. All good till now. No Errors.
But when I go and see my bundle in the felix console, I see that it is not Active but in Installed state.The error reported is that it could not resolve the javax.persistence package.
I was puzzled, I searched and I read about it here -
You have to make sure that you place the same version in another
bundle and deploy first. https://forums.adobe.com/thread/2325007
I converted JPA jar to OSGi bundle and installed in my OSGi container, and the error was gone. Great!
But why OSGi is not watching out for the dependencies I wrote in pom.xml of my maven project. Why it needs JPA strictly from OSGi bundle?
Maybe this is due to any architectural benefit, but could anyone please explain me here about this behaviour of OSGi? And why/how this feature of OSGi is useful ?
The <dependency> section of your Maven POM only covers your compile time dependencies. That means when you run Maven to build your project those dependencies are used to compile the source code and build your bundle. Maven itself is not aware of AEM or OSGi or any other platform or framework (e.g. Spring).
Maven just compiles your code.
You, as a developer, are responsible that all those required compile time dependencies are also available at runtime.
What we usually do is to create an AEM content package Maven module and put all of our required third party dependencies (e.g. JPA bundles) into it. This content package is then deployed by Maven so that those dependencies are also available at runtime.
Reason is: what you are adding as dependency is getting added in build path of your project and being available for your classes.When you run mvn install,it checks presence of all dependency and creates a bundle/jar for you.By default this bundle has only your project classes not other dependencies.
You need to check in depfinder whether external dependencies are already there in OSGi container,if not you have to load them in OSGi container either by embedding external dependencies in your bundle with the help of maven-bundle-plugin present in pom.xml or by making a bundle of jar file(I wont recommend that)which you have done.
I hope this helps!

include jar of one module to another

I have maven projet with this architecture:
++parent-project
+module-a
+module-b
module-b is a web application. it will be run on Jboss AS 7.1.1. I'm using netbeans IDE.
Now module-b depend on module-a. this is a porm section of module-b:
<dependency>
<groupId>groupid</groupId>
<artifactId>module-a</artifactId>
<scope>compile</scope>
</dependency>
When i build the war file of module-b, module-a is not present to lib folder ( in war file. i open it with archive explorer ). therefore JBoss return ClassNotFoundException.
I'm tried differends scope ( compile , provided , runtime , test ). But nothing.
Please how can i solve this.
First of all, I think you should try to see how does it work in "pure" maven, without the IDE at all (NetBeans). So my answer will be based only on maven knowledge:
A couple of facts:
Module b has to have the following in pom: <packaging>war</packaging> This will instruct maven that you really want to get a war from this module.
When packaging WAR is specified in some pom, maven will take all the dependencies defined in this pom and will put them into the WEB-INF/lib folder of the war. Automatically. Of course, you can customize the output, but its more advanced stuff (see Maven WAR plugin if required)
All the dependencies have to be defined with group id, artifact id, and version at least. So make sure that you have the dependency on module a with version. There is no need to fiddle with scopes in this case. The default scope (if you don't specify a scope at all) is 'compile' which is fine.
Go to the directory of module b and from within the directory type: mvn dependency:tree. Once its done, please carefully observe the output, especially make sure that module a is listed (with a correct version) in a tree.
Sometime to make sure that no stale artifacts reside in the local m2 repository you might want to delete all the jars of your project from there and then execute the mvn package command again. The war has to be created in module b/target - and this is the WAR you should check out.
Note, all these steps are done without any interaction with NetBeans at all.

ejb-client Maven dependencies in Eclipse

I use a dependency with ejb-client type in a Maven project:
<dependency>
<groupId>mygroup</groupId>
<artifactId>foo</artifactId>
<type>ejb-client</type>
</dependency>
It works properly when I build the project with Maven: it includes a version of the dependency that has remote EJB interfaces only (no implementations).
However, when I export the project to Eclipse Luna, and deploy it from Eclipse to jBoss/WildFly, Eclipse copies the full version of the dependency as if I used <type>ejb</type>.
Eclipse also gives me a warning:
Dependency to project foo with type ejb-client is not fully
supported. Classpath and/or deployment issues might arise. Try
Maven->Disable Workspace Resolution...
Is there a way to make ejb-client dependencies to work in Eclipse? Or is there any workaround?
In the past I've found that ejb-client jars are not a very good idea. They share the same dependencies as the full ejb-jar and you normally don't want that. You will also find that IDE support for these is a little weak.
You're far better off hand building your remote EJB API as a separate jar artefact and including it where needed.

How to prevent duplicate servlet jar using eclipse+m2eclipse

I'm using Eclipse + Maven + m2eclipse to build and test a web application in Apache Tomcat.
I've configured a Tomcat server inside Eclipse, and configured the deployment assembly for my web app, including "Maven Dependencies" (specialization of Java Build Path Entries).
When I deploy and start the server, Tomcat/Catalina always warns me:
INFO: validateJarFile(/projects/src/main/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webapp/WEB-INF/lib/servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
This is because m2eclipse sees servlet-api-2.5 as a dependency of my project, and considers it as part of "Maven Dependencies", and copies it as part of the deployment assembly, but the Tomcat servlet container has its own copy of this and doesn't like seeing 2 copies on the class path.
I've marked the dependency from my project to servlet-api-2.5 in my pom.xml with
<scope>
provided
</scope>
which does prevent standalone Maven from packaging servlet-api-2.5 into my builds, but m2eclipse doesn't see it that way.
(This isn't a huge deal because the warning is harmless, I only see it during testing inside the IDE, and real customers won't see it, but I'd still like to know how to fix it because I like cleanliness and I like knowing how things work.)
Is there a correct way to tell m2eclipse not to deploy this file, or to tell Eclipse not to let m2eclipse have the final say on which dependencies are runtime dependencies?
I did find https://issues.sonatype.org/browse/MNGECLIPSE-1193 which mentions
the "Maven Dependencies" container reflects test compile time scope, so it is supposed to have dependencies with scope "provided"
Install extras for m2eclipse plugin ("Maven Integration for WTP") from update site http://m2eclipse.sonatype.org/sites/m2e-extras. After install, update the project configuration.
Normally there is a dependency in your project that is depending on servlet-apî.jar
The default behaviour of Maven is that i wiill try to import your dependency + the dependencies of the imported dependency.
If you want to exclude a specific "sub-dependency", you can give maven a configuration like this :
<dependency>
<groupId>com.hpsworldwide.mbtrs.switch</groupId>
<artifactId>YOUR_DEPENDENCY</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
then maven will import YOUR_DEPENDENCY + all YOUR_DEPENDENCY dependencies, but will exclude servlet-api from the dependencies of YOUR_DEPENDENCY.
I have just had a similar problem, and believe I have got to the bottom of it.
If you go to your server configuration settings in Eclipse and select "Serve Modules without Publishing" then this should no longer occur.
Maven/M2Eclipse is building the WAR correctly - servlet-api-2.5.jar wont be in your target directories or WAR file.
But the problem is, when you deploy the application via eclipse on to your tomcat, Eclipse does not use your maven built WAR/target directories as default, it just uses the normal Eclipse "export" settings for your project. So it sees in your "Java EE Modules" (or "Deployment Assembly List" if you are using Helios) the list of all the jars in your Maven_Dependencies, but it does not respect the scope, and just deploys all the jars.
If you select serve without publishing option then Eclipst/Tomcat should just run the app straight off your target directory so will respect the maven scopes.
It won't ever affect your live deployments (unless you are deploying via eclipse!) as maven is doing the right thing, but it can sometimes cause problems locally as you can ave conflicting servlet/jsp jars which can cause classcastexceptions and general misery...
If you are using Indigo you can find the WTP plugin by clicking to "Window" -> "Preferences" -> "Maven" -> "Discovery" -> "Open Catalog".
One tip: after install "Maven Integration for WTP" and update the project configuration, check the directory /WEB-INF/lib and delete all JARs inside. Now, clean the Tomcat work dir and run again.

Resources