maven build helper plugin issue - spring

I'm using maven build helper to automatically add integration-test/java and integration-test/resources folders to my project classpath. It works great when I run my tests with JUnit in Eclipse, but got a problem when I ran the web project where Tomcat always pick up the test resources instead of the production one.
My project structure is something like this:
DaoProject
src/main/resources/datasource.properties
src/main/resources/spring-data.xml
src/main/java/....
WebProject
src/main/resources/appContext.xml
src/integration-test/java/com/mypkg/controller/MyControllerIT.java
src/integration-test/resources/datasource.properties
The appContext.xml imports spring-data.xml which in turn loads datasource.properties as a propterties source place holder.
appContext.xml contents:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config />
<tx:annotation-driven/>
<context:component-scan base-package="com.mypkg" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:defaultEncoding="UTF-8">
<property name="basenames">
<array>
<value>messages</value>
</array>
</property>
</bean>
<import resource="classpath:datasource.xml" />
datasource.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
>
<context:property-placeholder location="classpath:datasources.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${config.db.url}" />
<property name="username" value="${config.db.username}" />
<property name="password" value="${config.db.password}" />
pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-integration-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/integration-test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-integration-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/integration-test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

I think that the problem is with the way you load the datasource.properties file. Try specifying the entire path in the location attribute of the PropertyPlaceholderConfigurer:
<context:property-placeholder location="classpath:src/main/resources/datasources.properties"/>
Hope this helps.

Related

mvc:annotation-driven cause 404

In case I add
<mvc:annotation-driven />
to my applicationContext.xml, it causes 404 error on my Tomcat 8.
What is a reason and how can I debug this problem on Tomcat 8? Are any logs to help problem solving like this?
My applicationContext.xml:
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="pl.kobietydokodu" />
<context:annotation-config />
<mvc:annotation-driven /> <!-- this cause 404 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
I use Gradle to dependancy managament.

How to inject #PersistenceContext in DAO generated by Hibernate3-maven-plugin

I wanna create a Java Application based on Hibernate-3 and Spring Framework. To get the process easy I found hibernate3-maven-plugin that is able to perform reverse-engineering of the existing database.
Here there's example of POM:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2java</name>
<outputDirectory>src/main/java</outputDirectory>
<implementation>jdbcconfiguration</implementation>
</component>
<component>
<name>hbm2dao</name>
<outputDirectory>src/main/java</outputDirectory>
<implementation>jdbcconfiguration</implementation>
</component>
</components>
<componentProperties>
<revengfile>/src/main/resources/model.reveng.xml</revengfile>
<propertyfile>/src/main/resources/hibernate.properties</propertyfile>
<jdk5>true</jdk5>
<ejb3>true</ejb3>
</componentProperties>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>hbm2java</goal>
<goal>hbm2dao</goal>
</goals>
</execution>
</executions>
</plugin>
Then I set up the context of Spring:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="gomer" />
</bean>
<bean id="entityManager" factory-bean="entityManagerFactory" factory-method="createEntityManager"/>
<bean id="user" class="ru.tomtrix.first.db.UserHome">
<property name="entityManager" ref="entityManager"/>
</bean>
</beans>
It perfectly generates an Entity file and a DAO file except the following. In DAO file there's a EntityManager:
#Stateless
public class UserHome {
private static final Log log = LogFactory.getLog(UserHome.class);
#PersistenceContext private EntityManager entityManager;
... and this field hasn't got a setter! Eventually Spring throws the exception:
Invalid property 'entityManager' of bean class [ru.tomtrix.first.db.UserHome]: Bean property 'entityManager' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Of course it's not a good practice to write the setter manually. I think there is a way to inject a manager properly.
So how to do it without rewriting the generated file?
Corresponding information:
1) I'd like to create a stand alone application (and possibly run it in an Application Server like Tomcat)
2) model.reveng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM "http://www.hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<table-filter match-name=".*" package="ru.tomtrix.first.db"/>
</hibernate-reverse-engineering>
3) persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="gomer" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/users"/>
</properties>
</persistence-unit>
</persistence>
The problem is you are missing a part of the configuration. You need to tell Spring that you want to (also) use annotations for configuration. For this add the <context:annotation-config /> to your configuration and remove the setting of the entityManager
Next to that remove the calling of the factory method spring will handle all that for you.
Added tip use the version-less schemas insead of
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="gomer" />
</bean>
<bean id="user" class="ru.tomtrix.first.db.UserHome" />
</beans>
Your code might be problematic when you deploy it to a fullblown app server and you might run into issues with Spring and the EJB container competing over control of the beans. The hibernate plugin generates #Stateless session beans which in general will be picked up by the app. server (depending on which one you use).

Maven archetype: String replacement not working

I'm building a maven archetype. And filename substitution is working fine. For instance I have a file named __artifactd__-log4j.xml that when generated is replaced fine. But, one of the xml files, a spring context file, which references it, its not being replaced:
<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<context:annotation-config />
<bean id="log4jConfigurer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer"></property>
<property name="targetMethod" value="initLogging"></property>
<property name="arguments">
<list>
<value>classpath:${artifactId}-log4j.xml</value>
<value>5000</value>
</list>
</property>
</bean>
for some reason the token ${artifactId} inside the xml file is never replaced. My archetype-metadata.xml
<modules>
<module id="${rootArtifactId}-services" dir="__rootArtifactId__-services" name="${rootArtifactId}-services">
<fileSets>
<fileSet filtered="true" packaged="true" encoding="UTF-8">
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/webapp</directory>
<includes>
<include>**/*.jsp</include>
<include>**/*.xml</include>
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
</fileSets>
</module>
The spring-file is under src/main/resources, and since fileSet is set to filtered it should have being replaced by velocity right?
Any ideas?
Have you tried using ${project.artifactId}? According to the Maven Reference Book, all project properties have the project prefix.
Well, I was changing the template project I've used from mvn archetype:create-from-project instead of the generated-resources that's the reason.
So I ended having VLT doubled escaped.
Fixed :D

Weird behaviour with Spring config and Maven profiles

I have this weird behavior working with the Maven <filter> tag and Spring configs . My understanding is that a Spring config is a plain XML file to Maven but I'm encountering issues with the <context:component-scan base-package="com.xyz"/> tag . The test XML file is as below
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"
default-autowire="byName">
<!-- Import the DataSource configurations -->
<import resource="classpath:spring/MyDataSource.xml"/>
<!-- Property File location -->
<context:property-placeholder location="${ext.properties.dir}"/>
<!--The services are auto-detected POJOs labeled with the #Service annotation.-->
<context:component-scan base-package="com.xyz"/>
</beans>
and the Maven profiles configuration as below
<build>
.....
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>src/main/resources/build/build-${environment}.properties</filter>
</filters>
</build>
<profiles>
<profile>
<id>uat</id>
<activation>
<property>
<name>env</name>
<value>uat</value>
</property>
</activation>
<properties>
<environment>uat</environment>
</properties>
</profile>
<profile>
<id>prod</id>
<activation>
<property>
<name>env</name>
<value>prod</value>
</property>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment>prod</environment>
</properties>
</profile>
</profiles>
Contents of build-dev.properties are
ext.properties.dir=file:///C:/Temp/myProp.properties
My issue was that the Maven profile filtering was not working and the property ${ext.properties.dir} was not getting replaced during the packaging process . It stated working when I removed the <context:component-scan base-package="com.xyz"/> tag and hence I placed it below the property which needs to be filtered . Now everything works fine . My question is what's the issue with <context:component-scan base-package="com.xyz"/> ?
I do not think that <context:component-scan base-package="com.xyz"/> but the comment above
<!--The services are auto-detected POJOs labeled with the #Service annotation.-->
The # has a special meaning in maven fitlers.
To be honest I have the feeling that there are to many overlapping in syntax between spring configuration files and maven filters to use them together. My "solution" is to use (as long as possible) two files for the spring configuration.
a Property file, that is manipulated by spring filters
a normal Spring configuration file (with placeholders) that uses a PropertyPlaceholder Configurer to load the property file.

Apache Camel Spring configuration error - Unable to locate Spring NamespaceHandler for XML schema namespace [http://camel.apache.org/schema/spring]

I've posted an excerpt from my spring.xml file and the stack trace I'm getting.
<?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:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring-2.0.0.xsd"
default-init-method="init" default-lazy-init="false">
<!-- import web service configuration -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
<package>com.testdomain.testpackage</package>
</camelContext>
...
</beans>
Which throws this exception:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://camel.apache.org/schema/spring]
Offending resource: class path resource [spring-config.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:281)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1294)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1287)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:92)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:507)
at.......
Can somebody please help where the issue is?
For Camel versions < 3.8, add camel-spring.jar to your project. For versions after 3.8 add camel-spring-xml as well.
Using the spring xml dsl has been split off
If building a JAR with maven assembly plugin then try the shade plugin :)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.mypackage.mainclass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

Resources