Dependency listed in pom file not found in deployed project - maven

I asked a question here that I think I may have found the root of. I have a Spring Boot app using a datasource, net.sourceforge.jtds.jdbc.Driver, that is supposed to be included transitively by Spring Boot 2.0.2 with spring-boot-starter-jpa. However, when I run
jar tf my.jar | grep jtds
the driver class isn't found (we don't have a maven executable on the server to list the classpath). Everything I do to inspect the classpath reflects that the jar isn't there.
I've done this in 2 scenarios: 1) When I didn't explicitly add the jar to my pom, I got the error reported in my previous post. 2) When I do add it explicitly to the pom, I get this error:
java.lang.IllegalStateException: Cannot load driver class: net.sourceforge.jtds.jdbc.Driver
Can someone tell me what's going on?? I am confounded as to why this class can't be found and loaded.

Please mind, that in the Spring Boot Parent POM the jtds dependency is only included in test scope.
If you want to use classes of this dependency also in your production code, please change the Maven scope to compile.

Ok, the problem was solved by adding the dependency with a runtime scope.

In child pom where jar is packaged, you should have
spring-boot-maven-plugin. and dependency as below:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
</dependency>
In parent pom :
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>${jtds.version}</version>
</dependency>

Related

In Maven project, a class is found in local jar file at compile time, but not run time. why?

In a Maven project, I can include the classes with no problem.
However, during the runtime, I get ClassNotFoundException when I try to create an instance of that class.
Part of the pom.xml file:
<dependency>
<groupId>acmeGamesGroupId</groupId>
<artifactId>acmegames</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${acmegameslibs}\acme-1.0.jar</systemPath>
</dependency>
Why can a class be found during compile-time, but not in the run-time? What do I need to do in the pom.xml file? help.
You need more info. What is your execution environment: native java, tomcat, what? But my guess is your app is delivered as a jar file.
Unexpectedly the Java class loader cannot find classes in embedded jar files

javax validation can't find Hibernate Validator in Karaf

I've implemented some code using javax.validation and Hibernate Validator. The unit tests using validation are working fine. The build produces OSGi bundles and features, and runs in Karaf.
When I run my PaxExam integration test, I get "Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath." As far as I can tell, I AM adding it to my classpath. I have a features.xml file that I've been incrementally adding dependencies to. It finally got past Karaf bundle resolution, but now it's failing with an exception with this message:
"Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath."
I would include the stacktrace, but it doesn't look useful to me. Most of it is in my code, junit, and paxexam.
I'm attempting to use version 5.4.1.Final of HV. Note again that the unit tests doing validation are working fine. It took a while to get the dependencies correct to get that far (like using version "3.0.1-b08" of "javax.el".
I've seen some mentions of a "hibernate-validator-osgi-karaf-features" artifact, but I'm not sure if that's relevant. I'm attempting to use both the hibernate-validator artifact and the hibernate-validator-annotation-processor artifact.
I don't know that it's going to matter, but here's an excerpt of my POM dependencies:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>
hibernate-validator-annotation-processor
</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b08</version>
</dependency>
The following is an excerpt of a "features.xml" file from a bundle that is a dependency (by feature) of the bundle containing the test class:
<bundle start-level="100">wrap:mvn:javax.validation/validation-api/1.1.0.Final$Bundle-Name=javax.validation&Bundle-SymbolicName=javax.validation&Bundle-Version=1.1.0.Final</bundle>
<bundle>mvn:org.hibernate/hibernate-validator/5.4.1.Final</bundle>
<bundle start-level="100">wrap:mvn:org.hibernate/hibernate-validator-annotation-processor/5.4.1.Final$Bundle-Name=hibernate-validator-annotation-processor&Bundle-SymbolicName=hibernate-validator-annotation-processor&Bundle-Version=5.4.1.Final</bundle>
What else can I do at this point?
Update:
I've made some changes according to the answer that refers to the "hibernate-validator-osgi-karaf-features" artifact, but I'm now getting a different unexpected error.
In the pom dependencies I added the following:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>
hibernate-validator-osgi-karaf-features
</artifactId>
<version>5.4.1.Final</version>
<type>pom</type>
</dependency>
In the base features.xml file that is referred to by the features.xml file in my module, I removed the annotation-processor, and added this:
<bundle>wrap:mvn:org.hibernate/hibernate-validator-osgi-karaf-features/5.4.1.Final$Bundle-Name=hibernate-validator-osgi-karaf-features&Bundle-SymbolicName=hibernate-validator-osgi-karaf-features&Bundle-Version=5.4.1.Final</bundle>
I tried not having the "wrap:" and everything after the "$", but the result was the same.
When I ran my test, I saw this:
Caused by: shaded.org.eclipse.aether.transfer.ArtifactNotFoundException: Could not find artifact org.hibernate:hibernate-validator-osgi-karaf-features:jar:5.4.1.Final in central (http://repo1.maven.org/maven2/)
at shaded.org.eclipse.aether.connector.basic.ArtifactTransportListener.transferFailed(ArtifactTransportListener.java:39)[7:org.ops4j.pax.url.mvn:2.4.7]
at shaded.org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:355)[7:org.ops4j.pax.url.mvn:2.4.7]
at shaded.org.eclipse.aether.util.concurrency.RunnableErrorForwarder$1.run(RunnableErrorForwarder.java:67)[7:org.ops4j.pax.url.mvn:2.4.7]
at shaded.org.eclipse.aether.connector.basic.BasicRepositoryConnector$DirectExecutor.execute(BasicRepositoryConnector.java:581)[7:org.ops4j.pax.url.mvn:2.4.7]
at shaded.org.eclipse.aether.connector.basic.BasicRepositoryConnector.get(BasicRepositoryConnector.java:249)[7:org.ops4j.pax.url.mvn:2.4.7]
at shaded.org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(DefaultArtifactResolver.java:520)[7:org.ops4j.pax.url.mvn:2.4.7]
at shaded.org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:421)[7:org.ops4j.pax.url.mvn:2.4.7]
... 16 more
It's curious that it says it can't find it in central. I can verify the artifact is my local maven cache, because my build likely copied it there after I added the maven dependency as described above.
Update:
I'm guessing that part of my problem is that this artifact is a POM artifact, not a JAR artifact, but I don't understand how I need to reference it.
Update:
Someone on karaf-user pointed out that I need to reference it as a feature, not a bundle, so I now replaced my bundle references with the following:
<feature>hibernate-validator-osgi-karaf-features</feature>
Along with the following repository definition next to two other repository definitions:
<repository>mvn:org.hibernate/hibernate-validator-osgi-karaf-features/5.4.1.Final/xml/features</repository>
However, after installing the features file and then rerunning my test, it fails with this:
org.osgi.service.resolver.ResolutionException: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=usl-fraudcheck; type=karaf.feature; version="[2.5.0.SNAPSHOT,2.5.0.SNAPSHOT]"; filter:="(&(osgi.identity=usl-fraudcheck)(type=karaf.feature)(version>=2.5.0.SNAPSHOT)(version<=2.5.0.SNAPSHOT))" [caused by: Unable to resolve usl-fraudcheck/2.5.0.SNAPSHOT: missing requirement [usl-fraudcheck/2.5.0.SNAPSHOT] osgi.identity; osgi.identity=usl-base; type=karaf.feature [caused by: Unable to resolve usl-base/2.5.0.SNAPSHOT: missing requirement [usl-base/2.5.0.SNAPSHOT] osgi.identity; osgi.identity=hibernate-validator-osgi-karaf-features; type=karaf.feature]]
As I'm used to now, the last identity referenced here is the thing it can't find, unsurprisingly this new feature I'm referencing.
I then verified that the following file exists:
~/.m2/repository/org/hibernate/hibernate-validator-osgi-karaf-features/5.4.1.Final/hibernate-validator-osgi-karaf-features-5.4.1.Final-features.xml
However, I found it curious that it begins with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"
name="hibernate-validator-osgi-features"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.4.0">
The “name” property of the top-level features element is “hibernate-validator-osgi-features”, not “hibernate-validator-osgi-karaf-features”. Is that a problem?
Update:
I now understand that my features file has to reference a feature named "hibernate-validator", which is defined in that "hibernate-validator-osgi-karaf-features" artifact. That appears to have resolved my Karaf package resolution problems. However, that simply puts me back to my original problem of:
Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.
I tried changing the "LogLevel" in my Pax Exam config from WARNING to DEBUG. This gave me a lot more karaf debug output, but it didn't give me any significant info about why HV is not found in the classpath.
Is there some other debugging I can configure, or configuration in Pax Exam, that can help here?
Just take a look at our Karaf integration tests for the 5.4 branch:
https://github.com/hibernate/hibernate-validator/tree/5.4/osgi/integrationtest
We also use Pax Exam. You should be able to make it work if you follow what we did.
BTW, I see you mention the annotation processor in your dependency for the 2nd time, it's not something you need at runtime. The annotation processor is used to check that the annotations you used make sense at compile time. You should only enable it when you build your project.
See the Maven example here: https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-annotationprocessor-commandline .
This solved it for me:
https://access.redhat.com/documentation/en-us/red_hat_jboss_fuse/6.2/html/apache_cxf_development_guide/Validation#Validation-Intro-Resolver
Read from "Configuring the validation provider explicitly in OSGi"
The problem is that in OSGI CXF has trouble to find the provider automatically so you have to resolve manually by passing the hibernate validator as a constructor argument to the CXF Bean Validation Provider.
Hope this helps :)

Maven: The type cannot be resolved. It is indirectly referenced from required .class files

I changed some existing projects from ant to maven projects.
So far so good.
All projects do have the same groupId.
Theres a project with name "ServerBase" and artifactId "server-base".
Within this project theres an abstract class "BaseService" which defines a logger via:
import org.jboss.logging.Logger;
[...]
protected Logger log = Logger.getLogger(this.getClass());
Theres another project with name "Server" and artifactId "server".
Within this project theres a class ConfigurationDAOImpl extending the BaseService-Class above.
Within ConfigurationDAOImpl the logger log is used for creating some outputs.
Within the "Server"'s POM file I have declared:
<dependency>
<groupId>com.tcom.amadeus</groupId>
<artifactId>server-base</artifactId>
<version>${project.version}</version>
</dependency>
Under BuildPath the dependency is shown very nice under MavenDependencies. I removed the old dirct/natural/ant-dependency from build path before.
If I remove it I am getting very much errors about missing classes etc.
But although I do have this dependency I am getting the followin error in eclipse (under tab markers):
The type org.apache.commons.logging.Log cannot be resolved. It is indirectly referenced from required .class files
Resource: ConfigurationDAPImpl.java
Path: /Server/src/main/...
Location: Line 24
Type: Java Problem
I tried removing the dependency and add it again but without any luck.
Both projects do refer to JAVA 1.8.
Both projects have been build with targets clean an package multiple times.
Both projects have been updated by Righclick or pressing F5.
I am using Eclipse Version: Neon.1a Release (4.6.1)
I am using apache-maven-3.3.9
I am using m2e Plugin.
Any further help would be grateful.
Thanks in advance.
There are two ways to 'solve' this:
1)
explicitly add the required dependency within the server-projects pom-file:
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
2)
change the scop of the required dependency within the server-base-projects pom file from up to now 'provide' to 'compile' or erase the scope tag at all such that the default scope is used by maven (which I guess is 'compile')
old:
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<scope>provided</scope>
</dependency>
new:
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<scope></scope>
</dependency>
or:
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
Some background to this from documentation:
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Transitive_Dependencies
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.
Thanks all.
It looks like apache logging library is not brought transitively from your server-base project. Check if in project server under MavenDependencies you see commons-logging (apache logging) jar. If not, then add this as your maven dependency in server-base project.
Repeat the above for all jars that server-base depends on.

NoClassDefFoundError with jetty-maven-plugin

I am getting a:
he Cause java.lang.NoClassDefFoundError Msg:net/spy/memcached/MemcachedClient
When executing jetty:run -e in eclipse. Why isn't this dependency being added into the classpath?
Which classpath do you expect it to be added to? If something in your project is trying to load it, ensure you have a project dependency that has that class in it. It looks like it comes from ServiceMix. If you've added something to Jetty itself to make it require that class, then add the dependency to the jetty plugin.
Your code is missing a runtime dependency. I searched Maven Central for the missing class
http://search.maven.org/#search|ga|1|fc%3A%22net.spy.memcached.MemcachedClient%22
Try adding the following to your POM:
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.spymemcached</artifactId>
<version>2.5_2</version>
<packaging>bundle</packaging>
</dependency>
The dependency had a provided scope. Change this.

Maven 3 and JUnit 4 compilation problem: package org.junit does not exist

I am trying to build a simple Java project with Maven. In my pom-file I declare JUnit 4.8.2 as the only dependency. Still Maven insists on using JUnit version 3.8.1. How do I fix it?
The problem manifests itself in a compilation failure: "package org.junit does not exist". This is because of the import statement in my source code. The correct package name in JUnit 4.* is org.junit.* while in version 3.* it is junit.framework.*
I think I have found documentation on the root of the problem on http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html but the advice there seems to be meant for Maven experts. I did not understand what to do.
Just to have an answer with the complete solution to help the visitors:
All you need to do is add the junit dependency to pom.xml. Don't forget the <scope>test</scope>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
#Dennis Roberts: You were absolutely right: My test class was located in src/main/java. Also the value of the "scope" element in the POM for JUnit was "test", although that is how it is supposed to be. The problem was that I had been sloppy when creating the test class in Eclipse, resulting in it being created in src/main/java insted of src/test/java. This became easier to see in Eclipse's Project Explorer view after running "mvn eclipse:eclipse", but your comment was what made me see it first. Thanks.
my problem was a line inside my pom.xml i had the line <sourceDirectory>${basedir}/src</sourceDirectory> removing this line made maven use regular structure folders which solves my issue
removing the scope tag in pom.xml for junit worked..
I had the same problem. All i did was - From the pom.xml file i deleted the dependency for junit 3.8 and added a new dependency for junit 4.8. Then i did maven clean and maven install. It did the trick. To verify , after maven install i went project->properties-build path->maven dependencies and saw that now the junit 3.8 jar is gone !, instead junit 4.8 jar is listed. cool!!. Now my test runs like a charm.. Hope this helps somehow..
Add this dependency to your pom.xml file:
http://mvnrepository.com/artifact/junit/junit-dep/4.8.2
<!-- https://mvnrepository.com/artifact/junit/junit-dep -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.8.2</version>
</dependency>
I had my files at the correct places, and just removing <scope>test</scope> from the JUnit dependency entry solved the problem (I am using JUnit 4.12). I believe that with the test scope the dependency was just being ignored during the compilation phase. Now everything is working even when I call mvn test.
My case was a simple oversight.
I put the JUnit dependency declaration inside <dependencies> under the <dependencyManagement/> node instead of <project/> in the POM file. Correct way is:
<project>
<!-- Other elements -->
<dependencies>
<!-- Other dependencies-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<project>
I had a quite similar problem in a "test-utils" project (adding features, rules and assertions to JUnit) child of a parent project injecting dependencies.
The class depending on the org.junit.rules package was in src/main/java.
So I added a dependency on junit without test scope and it solved the problem :
pom.xml of the test-util project :
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
pom.xml of the parent project :
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
How did you declare the version?
<version>4.8.2</version>
Be aware of the meaning from this declaration explained here (see NOTES):
When declaring a "normal" version such as 3.8.2 for Junit, internally this is represented as "allow anything, but prefer 3.8.2." This means that when a conflict is detected, Maven is allowed to use the conflict algorithms to choose the best version. If you specify [3.8.2], it means that only 3.8.2 will be used and nothing else.
To force using the version 4.8.2 try
<version>[4.8.2]</version>
As you do not have any other dependencies in your project there shouldn't be any conflicts that cause your problem. The first declaration should work for you if you are able to get this version from a repository. Do you inherit dependencies from a parent pom?
Me too had the same problem as shown below.
To resolve the issue, below lines are added to dependencies section in the app level build.gradle.
compile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.5'
Gradle build then reported following warning.
Warning:Conflict with dependency 'com.android.support:support-annotations'.
Resolved versions for app (25.1.0) and test app (23.1.1) differ.
See http://g.co/androidstudio/app-test-app-conflict for details.
To solve this warning, following section is added to the app level build.gradle.
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:23.1.1'
}
}
I had a similar problem of Eclipse compiling my code just fine but Maven failed when compiling the tests every time despite the fact JUnit was in my list of dependencies and the tests were in /src/test/java/.
In my case, I had the wrong version of JUnit in my list of dependencies. I wrote JUnit4 tests (with annotations) but had JUnit 3.8.x as my dependency. Between version 3.8.x and 4 of JUnit they changed the package name from junit.framework to org.junit which is why Maven still breaks compiling using a JUnit jar.
I'm still not entirely sure why Eclipse successfully compiled. It must have its own copy of JUnit4 somewhere in the classpath. Hope this alternative solution is useful to people. I reached this solution after following Arthur's link above.
I also ran into this issue - I was trying to pull in an object from a source and it was working in the test code but not the src code. To further test, I copied a block of code from the test and dropped it into the src code, then immediately removed the JUnit lines so I just had how the test was pulling in the object. Then suddenly my code wouldn't compile.
The issue was that when I dropped the code in, Eclipse helpfully resolved all the classes so I had JUnit calls coming from my src code, which was not proper. I should have noticed the warnings at the top about unused imports, but I neglected to see them.
Once I removed the unused JUnit imports in my src file, it all worked beautifully.
Find the one solution for this error if you have code in src/main/java Utils
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.1</version>
</dependency>
Changing the junit version fixed this for me. Seems like version 3.8.1 didn't work in my case. Issue fixed upon changing it to 4.12
I met this problem, this is how I soloved it:
Context:
SpringBoot application
Use maven to manage multiple modules
Add junit's maven dependency in root POM's dependencyManagement(rather than dependencies, their differences can be found here)
Intend to test class or folder inside one of the root module's child module
PS: If your situation does not match the context above, this solution may not solve your problem.
Steps
right click at the class or folder you want to test:
Choose More Run/Debug -> Modify Run Configuration
Change the module option to the one you want to test from root module
By default , maven looks at these folders for java and test classes respectively -
src/main/java and src/test/java
When the src is specified with the test classes under source and the scope for junit dependency in pom.xml is mentioned as test - org.unit will not be found by maven.

Resources