JSF annotations ignored with imported project [duplicate] - eclipse

I have two JSF projects that share a lot of code - java classes, xhtml files, tag libraries, css and javascript files etc. My dev environment/platform consists mainly of Eclipse, Ant, Perforce and Tomcat.
Has anyone found a way to create and organize the shared code so that the common code can stay within one set of folders?
Eclipse makes it easy to add external folders for java sources, but falls short on the other file types. I'd appreciate any ideas.

Create a new "Java Project" in Eclipse. Add it as another project to the Deployment Assembly property of the main dynamic web project. This way it will automatically end up as a JAR in /WEB-INF/lib of the build of the web project. Since newer Eclipse versions, you can also create the project as "Web Fragment Project". This way the Deployment Assembly step will be done automatically.
Put all those shared resource files in /META-INF/resources folder of the Java project. Just treat it like WebContent/resources of the main web project. Tagfiles can just be kept in their own /META-INF/tags folder.
E.g.
CommonWebProject
|-- META-INF
| |-- resources
| | `-- common
| | |-- css
| | | `-- some.css
| | |-- js
| | | `-- some.js
| | |-- images
| | | `-- some.png
| | |-- components
| | | `-- somecomposite.xhtml
| | `-- sometemplate.xhtml
| |-- tags
| | `-- sometag.xhtml
| |-- beans.xml
| |-- faces-config.xml
| |-- some.taglib.xml
| |-- web-fragment.xml
| `-- MANIFEST.MF
:
with
<h:outputStylesheet library="common" name="css/some.css" />
<h:outputScript library="common" name="js/some.js" />
<h:graphicImage library="common" name="images/some.png" />
<common:somecomposite />
<common:sometag />
<ui:include src="/common/sometemplate.xhtml" />
...
In case you're using Maven, the /META-INF folder has to be placed in src/main/resources and thus NOT src/main/java!
If you want to trigger the JSF annotation scanner as well so that you can put #FacesValidator, #FacesConverter, #FacesComponent, #FacesRenderer and consorts in that project as well, then create a /META-INF/faces-config.xml file as well. Below is a JSF 2.3 compatible one:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
version="2.3">
<!-- Put shared faces-config.xml config here. -->
</faces-config>
The /META-INF/web-fragment.xml is mandatory for the JAR to be recognized by the servletcontainer as a "Web Fragment Project" and should already be generated by your IDE, but for sake of completeness here is how it should look like for a Servlet 4.0 compatible one:
<?xml version="1.0" encoding="utf-8"?>
<web-fragment
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-fragment_4_0.xsd"
version="4.0">
<!-- Put shared web.xml config here. -->
</web-fragment>
That's all.
See also:
Splitting up shared code and web.xml from WAR project to common JAR project
JSF facelets template packaging
Obtaining Facelets templates/files from an external filesystem or database

Related

ClassNotFoundException when deploying EAR on WebLogic Server

I'm trying to deploy an EAR on a WebLogic Server 12, which contains an EJB module (.jar) and a GUI module (.war). The structure of the EAR is the following :
+-- APP-INF
| +-- lib
| +-- jackson-annotations-2.5.0.jar
| +-- jackson-core-2.5.3.jar
| +-- jackson-databind-2.5.3.jar
| +-- jackson-dataformat-csv-2.5.3.jar
| +-- mapstruct-1.2.0.Final.jar
| +-- mapstruct-processor-1.2.0.Final.jar
+-- META-INF
| +-- application.xml
| +-- weblogic-application.xml
| +-- MANIFEST.MF
+-- jackson-annotations-2.5.0.jar
+-- jackson-core-2.5.3.jar
+-- jackson-databind-2.5.3.jar
+-- jackson-dataformat-csv-2.5.3.jar
+-- mapstruct-1.2.0.Final.jar
+-- mapstruct-processor-1.2.0.Final.jar
+-- my-ejb-module.jar
+-- my-gui-module.war
The content of my weblogic-application.xml is :
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-application
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-application"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/javaee_5.xsd
http://xmlns.oracle.com/weblogic/weblogic-application
http://xmlns.oracle.com/weblogic/weblogic-application/1.4/weblogic-application.xsd">
<!-- Paramètre l'encodage en UTF-8 -->
<wls:application-param>
<wls:param-name>webapp.encoding.default</wls:param-name>
<wls:param-value>UTF-8</wls:param-value>
</wls:application-param>
<!-- Activation du filtre classloader pour log4J -->
<wls:prefer-application-packages>
<wls:package-name>org.mapstruct.*</wls:package-name>
<wls:package-name>com.fasterxml.jackson.*</wls:package-name>
<wls:package-name>com.fasterxml.jackson.core.*</wls:package-name>
<wls:package-name>org.apache.log4j.*</wls:package-name>
<wls:package-name>javax.faces.*</wls:package-name>
<wls:package-name>javax.servlet.jsp.jstl.*</wls:package-name>
<wls:package-name>org.apache.taglibs.*</wls:package-name>
<wls:package-name>com.sun.faces.*</wls:package-name>
<wls:package-name>com.bea.faces.*</wls:package-name>
</wls:prefer-application-packages>
<wls:prefer-application-resources>
<wls:resource-name>org.mapstruct.*</wls:resource-name>
<wls:resource-name>com.fasterxml.jackson.*</wls:resource-name>
<wls:resource-name>com.fasterxml.jackson.core.*</wls:resource-name>
<wls:resource-name>javax.faces.*</wls:resource-name>
<wls:resource-name>javax.servlet.jsp.jstl.*</wls:resource-name>
<wls:resource-name>org.apache.taglibs.*</wls:resource-name>
<wls:resource-name>com.sun.faces.*</wls:resource-name>
<wls:resource-name>com.bea.faces.*</wls:resource-name>
<wls:resource-name>META-INF/services/javax.servlet.ServletContainerInitializer</wls:resource-name>
<wls:resource-name>META-INF/services/com.sun.faces.spi.FacesConfigResourceProvider</wls:resource-name>
</wls:prefer-application-resources>
</wls:weblogic-application>
But when I deploy the EAR via the wlfullclient.jar, I have the folowwing error :
Target state: deploy failed on Server xxx
java.lang.ClassNotFoundException: com.fasterxml.jackson.core.FormatSchema
But the class is located in the jackson-core-2.5.3.jar, maybe I am missing something ? I can not figure out what is the mistake here. Unfortunately, I have already searched on Google but the answers that I've found did not solve my problem.

Specifying a non-dll to be copied to all referencing projects' bin folders in a NuGet package

I have two solutions, Foo and Bar.
One of the projects in Foo, Foo.A, outputs a nuget package, which our build server pushes to an internal nuget feed. One of the projects in Bar, Bar.A, references this nuget package.
Foo.A contains a config file, which I want to have copied to the bin output folder of Bar.A upon build. To accomplish this, I've come up with the following nuspec:
<?xml version="1.0"?>
<package >
<metadata>
<id>Foo.A</id>
<!-- rest of metadata omitted -->
<contentFiles>
<files include="Config/*.config" buildAction="None" copyToOutput="true" flatten="false" />
</contentFiles>
</metadata>
<files>
<file src="bin\$Configuration$\*.dll" target="lib" />
<file src="bin\$Configuration$\*.pdb" target="lib" />
<file src="Config\*.config" target="lib\Config" />
</files>
</package>
This creates a package with an internal structure like this:
Foo.A.nupkg
|-- lib
| |-- Config
| | +-- Foo.A.config
| |-- Foo.A.dll
| +-- Foo.A.pdb
+-- Foo.A.nuspec
where the metadata section in the nuspec above is preserved in Foo.A.nuspec, but the files section is gone.
When I import this package in Bar.A and build, I get the dll and pdb file from Foo.A copied to the bin folder, but not the Config directory.
I've read the relevant sections of the nuspec reference a couple of times, but I'm not getting any wiser.
What do I need to do to get this working?

Deploying an EAR with EJBs and dependencies on JBoss

I'm trying to deploy a maven generated ear in a fresh installed Jboss EAP 6.1. The ear deploys well on Glassfish 2.x. The ear structure is this:
Ear_name-deploy-X.X.X.ear
|- META-INF
| |- maven
| | |
| | \-Ear_name -> Ear_name-deploy -> pom.properties
| | -> pom.xml
| |- application.xml
| |- MANIFEST.MF
| \- sun-application.xml
\- some jars (around 100)
Some of those jars are EJBs wich have dependencies on other jars in the same bundle and those dependencies are failing. I know I have to include a jboss-deployment-structure.xml file in META-INF folder. But I don't find how to define those dependencies without having to establish the jars as modules, what I don't want to do. Isn't there another way to stablish those dependencies? Thank you all for your time.

Integrate Maven with existing Java Project using Eclipse

I have created this Java project using struts, hibernate in Eclipse Helios,
Now I want to integrate this project with Maven how to do it?
I have installed Maven In Eclipse already.
Tutorials, blogs, websites that I have found so far are explaining integration of some project into Maven outside Eclipse and then importing it in Eclipse or crreation of New project with Maven.None of them so far addressing my Problem.
As I mentioned I have created a project in Eclipse already Now I just want to integrate it with Maven, how to do it?
In eclipse you can easily convert a java project in a maven one right clicking on project -> configure -> convert to maven project.
While an IDE "importer" can sometimes be handy, it is not required to turn a project in Eclipse into a maven project. Basically all you just need is to add a pom.xml file and follow maven's conventions - or configure it.
By using the maven-eclipse-plugin it is actually possible have a maven itself generate the necessary files to integrate your maven project with eclipse:
Start from the command line
Go to your project's root
Create a new pom.xml file from a simple template or initiate a new project folder structure (including a pom) using mvn archetype:generate
Type mvn eclipse:eclipse.
Then maven has generated the necessary files to integrate with eclipse.
That said, maven by convention expects a certain folder structure of your Java project. It looks like this:
my-app
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- mycompany
| `-- app
| `-- App.java
`-- test
`-- java
`-- com
`-- mycompany
`-- app
`-- AppTest.java
So unless you already have this structure, you need to move your source code to main/java (and unit test code to test/java).
Further if your project has dependencies to other projects; then you need to express these dependencies in Maven's pom.xml file. If your dependency projects are stored in the Maven Central this is particularly easy. To express a dependency to e.g. Apache Commons - you would add this to your pom.xml:
<project>
...
<dependencies>
...
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
...
</dependencies>
...
</project>
After these initial attempts to integrate your project with maven, you can try to build with mvn compile from either the command line - or using the m2eclipse plugin for Eclipse.

How to manage shared resources for several web apps in Maven AND Eclipse?

Note: I didn't get any response in the first version of this question, so I modified it to be more generic...
Context
My project is divided into several maven modules and several web-applications. Here is the structure:
my-project
+ pom.xml
+-- commons
+-- persistence
+-- ...
+-- web-app-1
+-- web-app-2
+-- ...
All the web applications share common resources, such as JS, CSS and images files.
Instead of duplicating these resources in each web-app-X, I decided to create another project called web-resources, which is a WAR project.
The structure is then the following one:
my-project
+ pom.xml
+-- commons
+-- persistence
+-- ...
+-- web-app-1
+-- web-app-2
+-- ...
+-- web-resources
+-- pom.xml
+-- src/main/webapp
+-- web.xml (which is almost empty, just need to be present for Maven)
+-- web_resources
+-- css
+-- images
+-- javascript
Maven
In Maven 2 (or Maven 3, as I just migrated my project to maven 3.0.2), this configuration is easy to manage as all web-app-X declare web-resources as a dependency:
<groupId>foo.bar</groupId>
<artifactId>web-app-1</artifactId>
...
<dependencies>
<dependency>
<groupId>foo.bar</groupId>
<artifactId>web-resources</artifactId>
<version>${preclosing-version}</version>
<type>war</type>
</dependency>
...
So when I build my WAR project, it first get the web-resources.war (built just before), unzip it, and build on top of it the web-app-X web-application.
This way, my WAR file will contains also a directory called web-resources/ that contains the shared resources.
This is the war overlay principle.
So on a Maven point of view, everything is fine!
Eclipse
Now, here comes the main problem: having a good Eclipse configuration.
Question: How can I use my current configuration to be managed correctly by Eclipse? In particular, when I deploy any web-app-X in Tomcat using Eclipse...
Note that I want to get the more automatizable (?) configuration, and avoid any manual steps, as this configuration should be used by dozens of developers...
For me, the best solution seems to use the linked resources of Eclipse. Thus, I set the following configuration in my web-app-X pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<wtpversion>1.5</wtpversion>
<linkedResources>
<linkedResource>
<name>web_resources</name>
<type>2</type>
<location>${project.basedir}\..\web-resources\src\main\webapp\web_resources</location>
</linkedResource>
</linkedResources>
</configuration>
</plugin>
...
When I run the mvn eclipse:eclipse configuration, it adds succesfully this information in my .project file:
<projectDescription>
...
<linkedResources>
<link>
<name>web_resources</name>
<type>2</type>
<location>C:\dev\project\web-resources\src\main\webapp\web_resources</location>
</link>
</linkedResources>
<projectDescription>
Now, I import my project in Eclipse.
Problem: in Project properties > Java Build Path > Source, I don't see the Link Source present.
I only see my four Maven default directories (src/main/java, src/main/resources, src/test/java and src/test/resources).
What is strange is that when I try to manually add the linked resources, it refuses and says that it already exists...
So when I deploy my web application on my Tomcat in Eclipse, it does not deploy the web_resources directory, and thus I don't get the CSS / JS / images deployed.
After some tests, it seems that I have to do two modifications:
Add the line <classpathentry kind="src" path="web_resources" output="src/main/webapp/web_resources"/> in my .classpath file;
Remove the <project>preclosing-web-resources</project> in the .project file.
Note that using this configuration, Eclipse will copy (and keep synchronization) the content of web_resources project in my web-app-X/src/main/webapp/web_resources, but this is not a problem (this directory is ignored by the SCM).
The only automated solution I found was to create a simple Maven plugin that do the two previous modification, and then run the following command (or use a .bat file):
mvn eclipse:clean eclipse:eclipse myEclipsePlugin:eclipse
Question
Is there a better way to manage such configuration?
Technical information
Java 6, Maven 3.0.2, maven eclipse plugin 2.8, Eclipse 3.3.2 (but I can test with newer version of Eclipse), no m2eclipse plugin.
Starting with Servlet 3.0, you can share resources by putting them within the src/main/resources/META-INF/resources directory.
When the webapp deploys, Servlet 3.0 makes those resources available from the context path. For example, in your case...
web-resources
-- src
---- main
------ resources
-------- META-INF
---------- resources
------------ css
-------------- global.css
------------ images
-------------- background.png
Let's assume that my_project has a dependency on web-resources and is deployed to the url http://my.localhost:8080/myProject.
With that configuration, the following URLs will resolve to the correct resources:
http://my.localhost:8080/myProject/css/global.css
http://my.localhost:8080/myProject/images/background.png
IF you have a name conflict between the resources and your actual application, the application will win.
Be sure your web.xml states you are using Servlet 3.0
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
You might want to take a look at this blog post. It covers a method for sharing resources using maven:
http://www.sonatype.com/people/2008/04/how-to-share-resources-across-projects-in-maven/