Maven, multi web modules shared Controllers, jsp, styles, scripts - maven

It's it possible with Maven to share common controllers, jsp's and other resources to the web module.
Structure
web1 (packaging jar)
--main
--java
--controller
MyControllerToShared with #Controller annotation
--resources
--webapp
--scripts
javascripts files
--styles
css files
--WEB-INF
--views
jsp to share
pom.xml
web2 (packaging war)
Classic web app structure with dependency of web1.jar
My web2 app works but no mapping found for HTTP request with URI define in web1 module. I use annotation #Controller and #RequestMapping. I defined in the servlet.xml:
<context:component-scan base-package="controller" />
<mvc:annotation-driven/>
How can I share controllers and resources between different web modules?
At the end, I need to have 3 web app with commons stuffs (error handler, jsp's errors, styles, js, ...).

Why don't you just create an extra project with all the shared resources? You can reuse that in all your web apps, just include the shared file in your pom.xml as a dependency.
You can start with an additional project with the shared resources:
<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>nl.connexys</groupId>
<artifactId>shared</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Shared resources</name>
</project>
In your webapp project you can then use that project as a dependency:
<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>nl.connexys</groupId>
<artifactId>webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Webservice endpoints</name>
<dependencies>
<dependency>
<groupId>nl.connexys</groupId>
<artifactId>shared</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
But this is all very basic Maven stuff. I think the examples should point you in the right direction.

Take a look at this answer:
https://stackoverflow.com/a/14143954/600007
, and the refernce for that: http://maven.apache.org/plugins/maven-war-plugin/overlays.html
It will work for jsp-s, js-s, etc...

Related

ivy.xml to pom.xml file

I am attempting to replace our internal Ivy repository with Artifactory and converting everything to Maven. The current repository is hosted on one of our servers in subversion and I am trying to get away from this. My biggest question has to do with the markdown of the ivy.xml and how it relates to pom.xml. I used the ant task makepom to convert but it seems to exclude the configurations and the publications section in the ivy.xml file. To be honest, I am new to ivy and maven and can't seem to find anything that explains this is maven terms. The particular file in question looks like this
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../../../../xsl/ivy-doc.xsl"?>
<ivy-module xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:noNamespaceSchemaLocation="../../../../xsd/ivy.xsd">
<info organisation="net.java.dev.jna" module="jna" revision="3.4.0" status="release" publication="20111210120000">
<license name="GNU Lesser General Public License, version 2.1 or later" url="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"/>
<ivyauthor xmlns:ivyde="http://ant.apache.org/ivy/ivyde/ns/" name="Ivy RoundUp Repository" url="https://github.com/archiecobbs/ivyroundup/blob/master/src/modules/net.java.dev.jna/jna/3.4.0/"/>
<repository xmlns:ivyde="http://ant.apache.org/ivy/ivyde/ns/" pattern="https://raw.githubusercontent.com/archiecobbs/ivyroundup/master/repo/modules/[organisation]/[module]/[revision]/ivy.xml" ivys="true" url="https://github.com/archiecobbs/ivyroundup" name="ivyroundup"/>
<description homepage="https://github.com/java-native-access/jna">
</info>
<configurations>
<conf name="core" description="JNA core library"/>
<conf name="platform" description="JNA platform library"/>
<conf name="default" extends="core,platform" description="JNA core + platform library"/>
</configurations>
<publications>
<artifact name="jna" ext="pom" type="pom"/>
<artifact conf="core" name="jna"/>
<artifact conf="core" name="jna-source" type="source" ext="zip"/>
<artifact conf="core" name="jna-javadoc" type="javadoc" ext="zip"/>
<artifact conf="platform" name="platform"/>
<artifact conf="platform" name="platform-source" type="source" ext="zip"/>
<artifact conf="platform" name="platform-javadoc" type="javadoc" ext="zip"/>
</publications>
The pom file I have created looks like so
<?xml version="1.0" encoding="UTF-8"?>
<!--
Apache Maven 2 POM generated by Apache Ivy
http://ant.apache.org/ivy/
Apache Ivy version: 2.4.0 20141213170938
-->
<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.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<packaging>jar</packaging>
<version>3.4.0</version>
<url>https://github.com/java-native-access/jna</url>
</project>
I've omitted some of the code connecting to our server. Trying to publish with maven publishes an empty jar file, I want to just publish the existing 3rd party jar. Any ways, if I could get some more clarification on migrating that publications block over to the pom file, that would be great.

display application version in title using thymeleaf and springboot

I want to display in my htm page the version of my webapp, using something like this (thymeleaf inside) :
<h4 th:text="${version}">Version</h4>
The data is well set in the 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>fr.test.navig</groupId>
<artifactId>navigo</artifactId>
<version>2.0.3-SNAPSHOT</version>
...
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Application</mainClass>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
and I can see it in the MANIFEST.MF (which is in the generated jar under META-INF) :
Implementation-Version: 2.0.3-SNAPSHOT
I've tried to get the appplication version in the controller and set it in a ModuleAttribute :
#ModelAttribute("version")
public String getVersion() {
logger.info("ModelAttribute to get application version");
return getClass().getPackage().getImplementationVersion();
}
But getClass().getPackage().getImplementationVersion() value is null. Indeed the package implementationVersion is not the implementation Version of the application by default.
I know I'm late but Patrick's answer and Spring docs greatly helps in this matter.
1. If your pom.xml use spring-boot-starter-parent as parent, you can use #project.version# to get version (and any other Maven properties) in your application.properties file. According to Spring docs:
You can automatically expand properties from the Maven project using
resource filtering. If you use the spring-boot-starter-parent you
can
then refer to your Maven ‘project properties’ via #..# placeholders
Maven pom.xml:
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Foo</name>
<description>Bar</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Spring application.properties:
foo.app.version=#project.version#
2. Then a class annotated with #ControllerAdvice can be used to inject version as model attribute.
#ControllerAdvice
public class ControllerAdvice {
#Value("${foo.app.version}")
private String applicationVersion;
#ModelAttribute("applicationVersion")
public String getApplicationVersion() {
return applicationVersion;
}
}
3. Finally this model attribute can be accessed by Thymeleaf as any other.
<th:block th:text="${applicationVersion}"></th:block>
Hope this helps!
Here is the simplest way I've found :
In my controller :
#ModelAttribute("version")
public String getVersion() throws IOException {
logger.info("ModelAttribute to get application version");
Manifest manif = new Manifest(
Application.class.getResourceAsStream("/META-INF/MANIFEST.MF"));
String version = (String) manif.getMainAttributes().get(
Attributes.Name.IMPLEMENTATION_VERSION);
return version;
}
In my htm page :
<h4 th:text="${version}">Version</h4>
You need to configure resource plugin to activate filtering on the file that need to be enriched with properties coming from your POM file.
In the generated war, the version (in fact ${project.version}) will be hardcoded to your POM version.

find path to JAX-RS resource

I just can't find out the path to access my JAX-RS resource which is deployed to wildfly.
ear.ear pom:
<parent>
<artifactId>jee-services</artifactId>
<groupId>org.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>ear</packaging>
<modelVersion>4.0.0</modelVersion>
<artifactId>ear</artifactId>
<dependencies>
<dependency>
<groupId>org.mycompany</groupId>
<artifactId>ejb_book</artifactId>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
</dependencies>
ejb_book pom:
<parent>
<artifactId>jee-services</artifactId>
<groupId>org.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>ejb</packaging>
<artifactId>ejb_book</artifactId>
application config
#ApplicationPath("/resources")
public class ApplicationConfig extends Application {
}
resource
#Stateless
#Path("/books")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class BookResource extends AbstractFacade<Book> {
#PersistenceContext
private EntityManager entityManager;
#GET
#Path("/getBooks")
public Book getBook() {
return new Book();
}
I think the issue is that I'm packaging my ejb_book.jar into ear.ear (where I collect all other ejb-modules)
I tried:
localhost:8080/ear/resources/books/getBooks
and many other combinations but none of them worked.
The application deploys fine to the WildFly server.
BTW: is there a tool to help people access their JAX-RS resource? From the IDE for example. So my question wouldn't be a problem anymore.
According to the JAX-RS 2.0 specification, section 2.3.2 Servlet
A JAX-RS application is packaged as a Web application in a .war file.
Since you're not packaging your application as a WAR module and neither you have a WAR module in your EAR, you're not providing a context-root to your (web) application.
This is preventing you from accessing your REST API through the desired way:
http://localhost:8080/<context-root>/resources/books/getBooks
As a solution, you could either package your entire application as a WAR, placing the application classes in WEB-INF/classes or WEB-INF/lib and required libraries in WEB-INF/lib, or you could maintain the use your EAR, adding to it a WAR module.

build CDT source code with m2eclipse

all,
I am trying to build one of CDT plugin and export it independently. For example, I built "org.eclipse.cdt.ui" plugin, and then I got the following built error message:
Project has not been setup yet MavenProject: org.eclipse.cdt:org.eclipse.cdt.ui:5.5.0-SNAPSHOT # C:\Documents and Settings\0359\workspace\org.eclipse.cdt\core\org.eclipse.cdt.ui\pom.xml org.eclipse.cdt.ui line 1 Maven Problem
I am headless with this problem, any suggestion is good. thanks.
its pom.xml is as following:
<?xml version="1.0" encoding="UTF-8"?>
<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>org.eclipse.cdt</groupId>
<artifactId>cdt-parent</artifactId>
<version>8.2.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<version>5.5.0-SNAPSHOT</version>
<artifactId>org.eclipse.cdt.ui</artifactId>
<packaging>eclipse-plugin</packaging>
< /project>

Causes for java.io.FileNotFoundException: class path resource [services.xml] cannot be opened because it does not exist

I have a web-application on RAD 7.5. I am building the application using Maven and deploying on WebSphere 6.1.
A part of my web.xml is:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:services.xml</param-value>
</context-param>
This is the only place where I am referencing services.xml.
My services.xml is in folder
FruitApplication/src/main/resources/services.xml
After building with maven, services.xml is in the following path in the target folder
target/FruitApplication-1.0.0/WEB-INF/classes/services.xml
On deployment, I continuously get below mentioned error. However, on building the application two three times, it disappears on its own and then reappears again haphazardly.
What is the actual cause of this error ?
How to solve this error ?
The top of my services.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
Is something needs to be changed here ???
Below is the complete pom.xml I am using for "maven-install":
<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>FruitApplication</groupId>
<artifactId>FruitApplication</artifactId>
<packaging>war</packaging>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<archive>
<manifestFile>
src/main/webapp/META-INF/MANIFEST.MF
</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
... dependencies here ...
</dependencies>
</project>
I have already tried all the below three options:
<param-value>classpath:services.xml</param-value>
<param-value>classpath:/services.xml</param-value>
<param-value>WEB-INF/classes/services.xml</param-value>
How I am deploying:
I am removing the application from the server by right clicking on server and "add or remove applications"
I am doing a run-as "maven-clean" and then run-as "maven-install" on the application and again adding the application by right clicking on the server and "add or remove applications"
Did you try classpath:/services.xml? The leading slash means to pull the resource from the root of the classpath. Without it, the file is expected to be in the package of whatever class is loading it (which I admit I don't know what that would be in this case).
See http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream%28java.lang.String%29
Select project where the reource files exist.Right click - Build path - configure build path - sources -Add folder - add the folder where you resource file exist.This would add the folder to you class path.
If the above project/jar is part of another WAR/EAR then select that WAR and/or EAR - properties - j2ee module dependecies and select the above project to get added to build path.
Restart the server.
Figure out if its Websphere/RAD or a application issue?
First build the war file and drop it in a Tomcat container and check if you still see the error.
1. If you see then copy the services.xml to WEB-INF and WEB-INF/classes directory and see if it works.
2. Also check your CLASSPATH system environment variable, system CLASSPATH can override your application classpath.
If everything is fine with Tomcat, there is something specific to Websphere/RAD:
3. Check you websphere environment, I recall there is some option in websphere where you can set/override/prefix the classpath.

Resources