Test phase in maven not picking up persistence.xml - java

I am trying to have a seperate persistence.xml for my tests, but it is not working. I have already looked at this post :
JPA using alternative "persistence.xml"
The post states that I just need to have a persistence.xml file in my src/test/resources/META-INF directory. This persistence file contains my JPA configuration for the test phase (In my case just the in-memory database).
However when I run Maven-->Test ALWAYS picks up the main/src persistence.xml file.
Do I need to configure something extra in my POM? My POM feels pretty sparse :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.glassfish.embedded.samples.ejbcontainer</groupId>
<artifactId>ejbcontainer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ejbcontainer</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Here's what it looks like in netbeans
Test phase always goes to the main persistence.xml :(

Related

How to inherit dependencies from other libraries in maven?

I created a custom my-commons library with maven. The commons pom contains eg the following dependency:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>de.mydomain</groupId>
<artifactId>my-commons</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<!-- workaround for http://jira.codehaus.org/browse/MASSEMBLY-603-->
<maven.build.timestamp>${maven.build.timestamp}</maven.build.timestamp>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
I installed the custom library into my local repository.
In a different project, I reuse the library. It resolves correctly, so it exists in the repo:
<project ...>
<groupId>de.mydomain</groupId>
<artifactId>my-core</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>my.domain</groupId>
<artifactId>my-commons</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Now in this project I'd like to use a class org.springframework.web.bind.annotation.ResponseBody.
Problem: the class is not in classpath. So maven complains [ERROR] cannot find symbol.
But why?? I'd expect the dependency being inherited!
Sidenote: I'm using Intellij IDEA if that matters. The same problem applies to all "inherited" libraries. The spring-web lib is just an example.
Please check if .m2 folder contains the jar you want to use or not. You can use mvn install command in commons project to generate the jar. Also you need to add commons project in your project pom that you are running.
You need to create the artifact for your commons project, through its pom.xml.
Like in my below example i created a separate project where i am using the AWS and some other dependency and then final jar, which i am using in other project :-
My AWS-client pom.xml looks like below :-
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kaamkaj</groupId>
<artifactId>aws-clients</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>aws-clients</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<aws.sdk.version>1.11.104</aws.sdk.version>
</properties>
<dependencies>
<!--AWS dependency-->
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.mercadopago</groupId>
<artifactId>sdk</artifactId>
<version>0.3.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>mercadopago</id>
<url>https://github.com/mercadopago/sdk-java/raw/master/releases</url>
</repository>
</repositories>
</project>
Also, run mvn clean install on the commons pom, in your case.
And then in all other project where i need to include the aws-client , i use the artifact-id of above project, like below :-
<dependency>
<groupId>com.kaamkaj</groupId>
<artifactId>aws-clients</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Let me know if you have any questions:
Found it: removing the <maven.build.timestamp>... <property> solved the problem. I don't know why it prevented the inheritance to work properly, especially as I didn't get any errors.

Reading GTFS-realtime with java : including an existing package

I want to read protobuffer GTFS-realtime inputs using java, and this package
https://github.com/google/gtfs-realtime-bindings/blob/master/java/README.md
I created first a java project using Maven:
mvn archetype:generate -DgroupId=com.me.app -DartifactId=myproject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd myproject
mvn package
...went fine.
According to the documentation of the GTFS-realtime binding package, the following should be included in the pom.xml:
<dependency>
<groupId>com.google.transit</groupId>
<artifactId>gtfs-realtime-bindings</artifactId>
<version>0.0.4</version>
</dependency>
In order to avoid duplicates (which made the compilation crashed) I removed the following lines:
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
so I got this pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.me.app</groupId>
<artifactId>myproject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myproject</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.google.transit</groupId>
<artifactId>gtfs-realtime-bindings</artifactId>
<version>0.0.4</version>
</dependency>
</dependencies>
</project>
The compilation then crashed. I am a newcomer in java and may have missed a point about how to include an existing package into a new project. Does anyone see what I am doing wrong?

Importing HBC library

I'm trying to import the following library: https://github.com/twitter/hbc
Which says I must import the folllowing in my pom.xml file:
<dependencies>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>hbc-core</artifactId> <!-- or hbc-twitter4j -->
<version>2.2.0</version> <!-- or whatever the latest version is -->
</dependency>
</dependencies>
I'm totally new to Maven. I'm using Eclipse, which already had Maven installed. I created a new Maven project, and then created a new .xml file named pom.xml with the above. However, nothing seems to have been imported; library stuff isn't working in the project. For example, the option to import "httpHosts" does not exist.
How am I meant to actually import this?
As Jorge Campos, suggests you need to do some reading on basic Maven. principles. So please read this in order to get started.
In order to help you a bit this is how your pom.xml should look like:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>hbc-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
You need to take care the groupid and artifact id sections, but I hope you will find out, if you spend some time and read about Maven :)

Can I create a Maven project that expects anyone who includes it to have an implementation of an arbitrary Java-ee 6 library?

I am creating a project that will be useful for anyone with a java-ee environment. I wan't to include the bare bones java-ee interface library in my project to allow me to code with all of the EJB annotations but i would rather make it's scope provided so as not to import all the classes into my project.
Is there a way I can make this dependency and require any user to have a java-ee library or do I have to make something like maven profiles that activate when it detects each server type, (i.e. one for jboss, one for glassfish, websphere, etc...).
my pom looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>project-that-wants-users-to-have-javaee-libraries</artifactId>
<version>0.0.1-Pre-alpha</version>
<dependencies>
....
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
EDIT: just to clarify what I Am asking, Say I have a maven project like so:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>testEJBProject</artifactId>
<dependencies>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-all-6.0</artifactId>
<version>3.0.2.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>project-that-wants-users-to-have-javaee-libraries</artifactId>
<version>0.0.1-Pre-alpha</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.4.Final</version>
</plugin>
</plugins>
</build>
</project>
will the dependency in my code be satisfied at runtime even though i'm specifically using the jboss-javaee-all-6.0 api and plugin?
It is my understanding that specifying the java api's and scoping them to provided will cause the dependency to expect anything using it to already have the implementations of those api's on the class path. you specify a dependency as provided like so:
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>tobeprovidedproject</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>

maven version dependency injection to sub modules

I purchased the 'Apache Maven 3 Cookbook' and I'm trying to learn on adding dependencies to my maven projects.
my main project pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven- 4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.srirangan.packt.maven</groupId>
<artifactId>TestModularApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>TestModularApp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies>
<modules>
<module>ChildProject</module>
<module>MyWebApp</module>
</modules>
</project>
as you can see here the packaging in this project is set to "pom" because this is the parent project.. ( that's all i know :) )
and then I created a subproject with the following pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.srirangan.packt.maven</groupId>
<artifactId>TestModularApp</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.xpogames.childproject</groupId>
<artifactId>ChildProject</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ChildProject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
as you can see here i did not add a version property to the mysql-connect-java dependency because it supposed to inherit the version from the parent project. but for some reason, it doesn't.
when I run mvn compile on that project i get an error that the dependencies.dependency.version property is missing.
any ideas what i'm doing wrong? how can I resolve the issue that i won't need to specify versions in sub-projects too ?
thanks!
update
after watching Peter Lawrey's answer and watching the example on the url he provided
I noticed that my main XML is missing the property <dependencyManagement> around the <dependencies>. once I added that property then I didn't need to provide a version number in the sub-project.
Peter answer shows another method to achieve this goal.
thanks for everything!
Kfir
You inherit versions from the dependencies in the dependencyManagement section of your parent pom(s) and selectively include these in child poms.
In your case you don't need to mention the dependency again as it already included from the parent. (As it is for JUnit)
BTW: I use JUnit 4.10 which I haven't found any backward compatibility problems with.

Categories