Trying to get Roboguice working in Eclipse which contains 2 Maven projects, Astroboy and Roboguice (let's call them A and B where A depends on B). My problem is that unit tests in A which work fine in Maven give compile errors in Eclipse because it can't find Junit. Junit is declared in A's pom as a provided dependency, as below. I did tell Eclipse that project A depends on B by adding B to A's build path/projects.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>provided</scope>
</dependency>
I got it to work temporarily by changing the scope to test (then doing a mvn install and maven/update project on A), but what must I do to have it find Junit by itself?
Related
I'm just going through a tutorial which is a Maven project in Eclipse, and it should run some tests using JUnit4, so I have put this dependency in the POM:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
Still, trying to run the tests I get the following:
No tests found with testrunner 'JUnit5'
Why is Eclipse not using JUnit4 when this is the only dependency given in the project?
Because in the run configuration you have chosen JUnit 4 as Test Runner, Eclipse uses JUnit 4 instead of JUnit 5 to run this test.
Please note that even though Eclipse preselects the matching Test Runner for new run configurations, it does not change the Test Runner for existing run configurations.
I have some test-classes under the folder src/test/java (some of these having name ending by *Test). Each class has some methods annotated with #Test
Running these tests with JUnit (on Eclipse, right click on the class, then run as, the JUnit Test) I have no problems. But I want to run these tests using mvn test.
The problem is that I obtain always this:
It seems that mvn finds the tests, but it doesn't execute them. Why?
Furthermore, it seems that also classes having a name that doesn't end with *Test are considered by Maven.
This is part of my pom.xml:
And this is part of my effective pom:
You are using a very outdated version of JUnit.
In JUnit 3.x, tests did not use annotations; the method names had to start with test*.
In JUnit 4.x, test methods had to be annotated with #Test, which is what you apparently have.
In your pom, you need to upgrade JUnit:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
In my pom.xml, I added this dependency:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<scope>provided</scope>
</dependency>
<!-- Annotation processor that raising compilation errors whenever constraint
annotations are incorrectly used. -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<scope>provided</scope>
</dependency>
When I compile and install the project by executing "mvn clean install" at terminal, the model classes are generated in this directory:
target/generated-sources/annotations/com/myproject/ne/model/
Then if I import this Maven project from Eclipse, it works all fine without complaining the model classes automatically generated.
However, if I don't execute "mvn clean install" at a terminal to generate the model classes and directly import the clean project, Eclipse doesn't generate the model classes, and therefore generates compilation errors.
What's needed to use the hibernate-jpamodelgen to automatically when a clean maven project is imported and compiled?
You can achieve it by enabling it with annotation processing.
In eclipse right click on project --> properties --> Java Compiler --> Annotation Processing --> Factory Path enable it.
Now populate the .factorypath file at the root of your project with the following contents:
<factorypath>
<factorypathentry kind="PLUGIN" id="org.eclipse.jst.ws.annotations.core" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/hibernate/hibernate-jpamodelgen/1.0.0.Final/hibernate-jpamodelgen-1.0.0.Final.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.0.Final/hibernate-jpa-2.0-api-1.0.0.Final.jar" enabled="true" runInBatchMode="false"/>
</factorypath>
Refresh the project in Eclipse
Follow #Tanvi B answer, and 1 more step:
project --> properties --> Java Compiler --> Annotation Processing:
Enable project specific settings (check all). And at Generated source directory, you have to define target/generated-sources
Maven compilation failes even after adding the external jars to eclipse. My Eclipse codes are okay with external jars, however when I compile Maven complains package blah blah not found, and I have almost 50 external packages.
I will use the mvn dependency to add the jars later.
However It should work, but not luck.
Any troubleshooting/suggestion please.
I think you can not get away with Maven Project without having <dependencies> tag in your pom.xml (Whether you run it from eclipse or from command-line) . That too for the project which is dependent on classes that are coming from 50 external jars.
If you don't want maven to look for these jars, you have to add below entry in pom.xml with system scope:
<dependencies>
<dependency>
<groupId>selenium-server-standalone</groupId>
<artifactId>selenium-server-standalone</artifactId>
<version>2.46.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/selenium-server-standalone-2.46.0.jar</systemPath>
</dependency>
</dependencies>
With this, groupID & artifactID are meaningless. You can write such 50 <dependency> tags for referring your 50 external jars.
Before importing my Maven project, I build it using the following: mvn clean install
I then create the necessary Eclipse files as follows: mvn -Dwtpversion=2.0 eclipse:eclipse
I notice that my Referenced Libraries in Eclipse contains this jar: validation-api-1.0.0.GA.jar
However, when I view the effective POM in Eclipse no such jar appears. Therefore, I am wondering how this jar gets added to my Eclipse classpath?
I require this jar for #Valid annotation I am using and I need Maven to be aware of it. If I build my classpath files using Maven then how come Maven is not aware of it?
Thanks
Check the Dependency hierarchy-tab in your pom (in Eclipse), maybe the jar is a dependency of some other jar you use.
In this case, the reference was in the project's Java Build Path, and was probably added when the project was created because of the -Dwtpversion=2.0 -parameter.
Maven also adds to your class path the sub-dependencies of your main dependencies (which are those specifically declared by you in the pom.xml). Do a
mvn dependency:tree -Dverbose
To see what other dependencies are pulled in with a specific pom-declared dependency.
Also, if you only wanna see the subdependencies of a certain dependency, called x.y.z you can do:
mvn dependency:tree -Dverbose -Dincludes=x.y.z
(where x.y is the groupId and z is the artefactId)
One of your project dependencies probably has a dependency for this jar file. Check the graphical dependency graph or just search for that, find out your project dependency that is dependent on this and exclude this dependency, if possible, by using maven's 'exclude' tags.
You can use maven dependency exclusions, as below:
<project>
...
<dependencies>
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Source: Maven - Dependency Exclusions
I have encountered the same problem. And after some research I realized that transitive dependencies of your parent pom may not show up in Effective POM, but would be present in Reference libraries.
The whole confusion raised because when I ran the following command
mvn dependency:tree -Dincludes=X (where X is the group-id of the jar I was looking for)
It did no mention of parent pom, instead it referred to dependency in parent pom which brings X to the table. (Which make sense because we inherit from parent pom).