how to import java files from ejb module in web app? - maven

i create ear project and i have 2 module named:
mavenproject4-web
mavevnproject4-ejb
i try to import some interface and entity from ejb module in web module.But i can't.
i tried to add ejb module as dependency into web module by pom.xml.Then my problem fixed.Is it right way?
after adding dependency i tried to run project and i got this exception:
Severe: Exception while preparing the app : Could not resolve a persistence unit corresponding to the persistence-context-ref-name [mysessionbeans.StudentDAOImpl/entityManager] in the scope of the module called [mavenproject4-ear#mavenproject4-ejb-1.0.jar]. Please verify your application.
my persistence xml is in "src/main/resources" in ejb module.Detail information about project structure in image:
My web app 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>
<parent>
<artifactId>mavenproject4</artifactId>
<groupId>az.lsim</groupId>
<version>1.0</version>
</parent>
<groupId>az.lsim</groupId>
<artifactId>mavenproject4-web</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>mavenproject4-web</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>az.lsim</groupId>
<artifactId>mavenproject4-ear</artifactId>
<version>1.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Thanks

The ejb jar file will need to be in the ear file. But I also think that the persistence.xml file needs to be in META-INF folder in the EJB jar file. I suspect that the way you have it there, it's in the root folder. Try moving it to src/main/resources/META-INF/persistence.xml
See http://docs.oracle.com/cd/E14101_01/doc.1013/e13981/pkgapp001.htm

Related

How to use user-defined Java function in velocity files in archetype-resource path?

I am creaing a arhetype project for convenient creation of similar projects by maven-archetype-archetype archetype. The following is the command I use:
mvn archetype:generate \
-DgroupId=cn.edu.idea.bios \
-DartifactId=archetype-spring-console \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-archetype \
-DarchetypeVersion=1.4
As shown in the picture above, I want to use a Java function in a velocity file in the archetype-resources path. There is a ClassNotFoundExcept thrown when I executed the command mvn clean install.
The class is located in target/classes path.
This is my pom.xml. the compiler plugin is added by myself because of no classes found in target/classes before adding the compiler plugin.
How to resolve this problem, please?
<?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>cn.edu.idea.bios</groupId>
<artifactId>archetype-spring-console</artifactId>
<version>1.0.0</version>
<packaging>maven-archetype</packaging>
<name>Archetype - archetype-spring-console</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>3.0.1</version>
</extension>
</extensions>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.2.1</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

OSGi: How to embed dependencies in a wrapper bundle

We want to use the google-maps-services in our AEM project. To be able to use it in the OGSi environment I creates a wrapper bundle that embeds the jar and exports the com.google.maps package.
Sadly the bundle cannot be started, because it is missing the following dependencies:
com.google.appengine.api
io.opencencus.stats
io.opencencus.tags
okhttp3
okio
I tried to embedd them as well, but for some reason this doesn't seem to work as well.
What am I doing wrong?
<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>
<parent></parent>
<artifactId>thirdparty-google-maps-services-bundle</artifactId>
<name>aem-main - Thirdparty - google-maps-services-bundle</name>
<description>Wrapper bundle for Google Maps API</description>
<build>
<plugins>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<executions>
<execution>
<id>bnd-process</id>
<goals>
<goal>bnd-process</goal>
</goals>
<configuration>
<bnd><![CDATA[
Bundle-Category: thirdparty
Import-Package: javax.annotation;version=0.0.0
Export-Package: com.google.maps.*
Embed-Dependency: google-maps-services,okhttp,okio,opencensus-api,appengine-api-1.0-sdk
-exportcontents: ${packages;VERSIONED}
-snapshot: ${tstamp;yyyyMMddHHmmssSSS}
]]></bnd>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-baseline-maven-plugin</artifactId>
<configuration>
<failOnMissing>false</failOnMissing>
</configuration>
<executions>
<execution>
<id>baseline</id>
<goals>
<goal>baseline</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>sling-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- ====================================================================== -->
<!-- D E P E N D E N C I E S -->
<!-- ====================================================================== -->
<dependencies>
<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.18.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.4</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.17.2</version>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
<version>0.25.0</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>1.9.88</version>
</dependency>
</dependencies>
</project>
After many try and errors, I found what was missing. You need to export the other dependencies as well:
Export-Package: io.opencensus.tags,io.opencensus.stats,com.google.appengine.api.urlfetch,okhttp3.*,okio.*com.google.maps.*
And you need to import this for runtime:
Import-Package: javax.net.*;version=0.0.0

How to generate a SINGLE jar contains /src/main/java and /src/test/java

I have created a maven project and trying to generate a SINGLE jar file should contain both /src/main/java & /src/test/java.
But it is generating two SEPARATE jar files. Please let me know, how can I achieve this in maven?
generated jar files:
test-0.0.1-SNAPSHOT.jar
test-0.0.1-SNAPSHOT-tests.jar
my 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testmaven</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
You cannot access test classes from application code. So no maven plugin will help you to add src/main/java and src/test/java in a single executable jar.
If at all you want to access the main classes from the test project, check the answer similar to your question on stackoverflow here:
How can I include test classes into Maven jar and execute them?
I have tried with below approach and it worked.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/test/java/</source>
</sources>
</configuration>
</execution>
</executions>

What dependency scope for Derby JDBC drivers in intergration scope (cayenne)

I am trying to build my pom.xml so that I can automatically create my database schema when running 'mvn install'. I'm using the "maven-cayenne-plugin" to do this. This is plugin is being called (at the integration-test phase), as I can see the output. But the mojo fails with the exception: (I used the -e and -X flag to see this).
java.lang.ClassNotFoundException: org.apache.derby.jdbc.ClientDriver
(I get the same if I try and use the EmbeddedDriver and whether or not I include 'derbyclient' or simply 'derby' as my dependency).
Here's a pom.xml that should replicate the issue.
I'm using MVN 3 on Windows.
[ Apache Maven 3.0.4 (r1232337; 2012-01-17 08:44:56+0000) ]
<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</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.cayenne.plugins</groupId>
<artifactId>maven-cayenne-modeler-plugin</artifactId>
<version>3.2M1</version>
</plugin>
<plugin>
<groupId>org.apache.cayenne.plugins</groupId>
<artifactId>maven-cayenne-plugin</artifactId>
<version>3.2M1</version>
<executions>
<execution>
<id>cgen</id>
<configuration>
<superPkg>com.mycompany.model.generated</superPkg>
<map>${project.build.sourceDirectory}/../resources/datamap.map.xml</map>
<destDir>${project.build.sourceDirectory}</destDir>
</configuration>
<goals>
<goal>cgen</goal>
</goals>
</execution>
<execution>
<id>cdbgen</id>
<configuration>
<map>${project.build.sourceDirectory}/../resources/datamap.map.xml</map>
<driver>org.apache.derby.jdbc.ClientDriver</driver>
<url>jdbc:derby:memory:tracedb;create=true</url>
<username>test</username>
</configuration>
<goals>
<goal>cdbgen</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.10.1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
This also requires a valid cayenne "datamap.map.xml" file (in src/main/resources), here's one I made earlier:
<?xml version="1.0" encoding="utf-8"?>
<data-map xmlns="http://cayenne.apache.org/schema/3.0/modelMap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://cayenne.apache.org/schema/3.0/modelMap http://cayenne.apache.org/schema/3.0/modelMap.xsd"
project-version="6">
<db-entity name="TEST">
<db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
</db-entity>
</data-map>
EDIT:
Adding more information.
The derbyclient-10.10.1.1.jar does contain the class 'org.apache.derby.jdbc.ClientDriver' (just expanded the JAR from Netbeans).
The -X flag seems to show that the CLASSPATH is correctly referencing the JAR:
[DEBUG] (f) classpathElements = [<PROJECT-PATH>\mvn\target\classes, <HOME-DIR>\.m2\repository\org\apache\derby\derbyclient\10.10.1.1\derbyclient-10.10.1.1.jar]
SOLUTION:working pom.xml (see answer and my comment):
<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</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.cayenne.plugins</groupId>
<artifactId>maven-cayenne-modeler-plugin</artifactId>
<version>3.2M1</version>
</plugin>
<plugin>
<groupId>org.apache.cayenne.plugins</groupId>
<artifactId>maven-cayenne-plugin</artifactId>
<version>3.2M1</version>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.10.1.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>cgen</id>
<configuration>
<superPkg>com.mycompany.model.generated</superPkg>
<map>${project.build.sourceDirectory}/../resources/datamap.map.xml</map>
<destDir>${project.build.sourceDirectory}</destDir>
</configuration>
<goals>
<goal>cgen</goal>
</goals>
</execution>
<execution>
<id>cdbgen</id>
<configuration>
<map>${project.build.sourceDirectory}/../resources/datamap.map.xml</map>
<driver>org.apache.derby.jdbc.EmbeddedDriver</driver>
<url>jdbc:derby:memory:tracedb;create=true</url>
<username>test</username>
</configuration>
<goals>
<goal>cdbgen</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.10.1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
To ensure that the Derby driver is available during plugin execution (vs during your code compilation), you need to add it as a dependency of the plugin itself:
<plugin>
<groupId>org.apache.cayenne.plugins</groupId>
<artifactId>maven-cayenne-plugin</artifactId>
<version>3.2M1</version>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.10.1.1</version>
</dependency>
</dependencies>
....
</plugin>

External jar found in Maven Web Application but not when being deployed

I recently created a Maven Web Application through Netbeans 7.3, using GlassFish 3.1.2. In this I use an external jar, so I added it in pom.xml:
<dependencies>
...
<dependency>
<groupId>be-fedict-eid-trust-service-client</groupId>
<artifactId>eid-trust-service-client</artifactId>
<version>1.0.1.RC5</version>
</dependency>
...
</dependencies>
It shows up correctly and I can refer it in my Bean. BUT when I deploy the application, I get an error that the class in the jar (which I referred to without problems before) can not be found.
java.lang.NoClassDefFoundError: be/fedict/trust/xkms2/XKMSServiceFactory
Since I'm not that familiar with Maven, I don't know how and where I can fix this. Looking at the generated WAR file, the jar is there correctly. I set addClasspath to true so it is in the Classpath of the Manifest, but this doesn't seem to help.
My libraries are in WEB-INF/lib and my Bean is in WEB-INF/classes.
Any thoughts or general directions to what this problem may be? I found this topic: Spring Web App with Maven dependency - class not found during deploy
but I don't see how I can make it work for me (assuming he found a solution).
Thanks in advance!
My full 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ISB</groupId>
<artifactId>eID</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>eID</name>
<repositories>
<repository>
<id>e-contract</id>
<url>https://www.e-contract.be/maven2</url>
</repository>
</repositories>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>be-fedict-eid-applet</groupId>
<artifactId>eid-applet-shared</artifactId>
<version>1.1.0.RC2</version>
</dependency>
<dependency>
<groupId>be.fedict.eid-applet</groupId>
<artifactId>eid-applet-service</artifactId>
<version>1.1.0.RC2</version>
</dependency>
<dependency>
<groupId>be-fedict-eid-applet</groupId>
<artifactId>eid-applet-service-spi</artifactId>
<version>1.1.0.RC2</version>
</dependency>
<dependency>
<groupId>be-fedict-eid-applet</groupId>
<artifactId>eid-applet-package</artifactId>
<version>1.1.0.RC2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>be-fedict-eid-trust-service-client</groupId>
<artifactId>eid-trust-service-client</artifactId>
<version>1.0.1.RC5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy</id>
<phase>process-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>be-fedict-eid-applet</groupId>
<artifactId>eid-applet-package</artifactId>
<version>1.1.0.RC2</version>
<type>jar</type>
<outputDirectory>${project.build.directory}/${project.artifactID}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I'm not sure it has anything to do with your POM. I suspect it's the libraries used in the target container (Glassfish) -- a classpath issue. Each container does classloading a little differently. This question & answer might give you some ideas. If there are two, different versions of this artifact, one in container, one in your POM, you will have to tell the container which to use. WebLogic uses a weblogic.xml file for this.

Resources