Drools---JUnit Testing - Drools Spring does not scan packages from source folders - spring

I have a situation. I am testing bunch of .drl files using the kie-spring. The DRL files are found/scanned only when they are co-located in the src/test/resources folder and not in the src/main/resources/ folder.
I even moved the drl files to a separate jar project/file into a src/main/resources folder along with the kie-spring .xml files. Still no luck! The following is the warning I get!
2014-09-30 15:24:51,227 [AbstractKieModule] [main] WARN No files found for KieBase MASTRT_KBase, searching folder \Users\mmadhavan.FACS_ORG\workspace\org.ncdb.facs.measures\target\test-classes
The following is my xml file!
<?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:kie="http://drools.org/schema/kie-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd">
<kie:kmodule id="kbase_inlist_op_test_rules">
<kie:kbase name="MASTRT_KBase" packages="org.xxx.xxx.xxxxx.drl.cancer.MASTRT">
<kie:ksession name="MASTRT_KSession" type="stateless" scope="prototype"/>
</kie:kbase>
</kie:kmodule>
<bean id="kiePostProcessor" class="org.kie.spring.annotations.KModuleAnnotationPostProcessor"/>
</beans>

I solved adding src/main/resources to testResources in pom.xml
<build>
<testResources>
<testResource>
<directory>${basedir}/src/test/resources</directory>
</testResource>
<testResource>
<directory>${basedir}/src/main/resources</directory>
</testResource>
</testResources>
</build>

Related

Maven profile parameters aren't loaded in Spring JUnit context

I'm running JUnit tests with Maven profile.
Maven profile looks so:
<profile>
<id>someProfile</id>
<properties>
...
<some.param>some_value</some.param>
...
</properties>
</profile>
Spring context file(testContext.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
...
xmlns:p="http://www.springframework.org/schema/p"
...
xsi:schemaLocation="...">
<bean id="someBean" class="someClass"
scope="singleton"
autowire="byName"
init-method="init"
p:someBeanParam="${some.param}"/>
</beans>
And test class begins so:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:/testContext.xml"})
#Configurable
...
After running maven, I saw that testContext.xml wasn't changed - p:someBeanParam still had value ${some.param}.
Could you tell, please, what's the problem here and how to solve it?
Thank you in advance.
Enable resource filtering like this
<project>
...
<build>
...
<resources>
...
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
</resources>
<testResources>
...
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
...
</testResources>
...
</build>
....
Define variable for each profile and when no profile is used.
More information on Maven Resource Plugin page.

Create file in project directory with archetype plugin

I want to place a README.md(and maybe some other files) next to the pom.xml of the project that is created by the Maven Archetype plugin.
It seems that it is only allowed to place files
<sources> = src/main/java
<resources> = src/main/resources
<testSources> = src/test/java
<testResources> = src/test/resources
<siteResources> = src/site
whereas I want to place files in .. How can I do this?
To clarify what user1811587 is saying, if you are using an archetype-metadata.xml file, like the one created when generating an archetype through mvn archetype:create-from-project, the format would be:
<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" name="viewport-bootstrap"
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fileSets>
<fileSet filtered="true" packaged="false" encoding="UTF-8">
<directory/>
<includes>
<include>README.txt</include>
</includes>
</fileSet>
</fileSets>
</archetype-descriptor>
The above XML will place the README.txt along side the pom.xml.
something like this
<resources>
<resource>README.txt</resource>
</resources>
should help you.

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.

Running a Spring MVC application with Jetty gives "class path resource does not exist"

I'm new to Spring and I encountered a small problem: the web application runs perfectly when using Tomcat but has problem when running it with Jetty.
I run the following commands:
mvn package
java -jar target/dependency/jetty-runner.jar target/*.war
The error I get is:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist
Part of my 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>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
...
</dependencies>
<repositories>
...
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-runner</artifactId>
<version>7.4.5.v20110725</version>
<destFileName>jetty-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Part of my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd"
version="2.5">
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
...
</web-app>
Part of my /WEB-INF/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:context="http://www.springframework.org/schema/context"
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">
...
<import resource="classpath:spring-config.xml" />
</beans>
The relevant directories structure:
- src
--main
---java
----spring-config.xml
---webapp
----WEB-INF
-----applicationContext.xml
-----web.xml
-pom.xml
Seems to be a problem with the classpath definition but I don't know how to solve the problem. I already tried to specify the classpath with java -cp "..." ... or java -Djetty.class.path="..." ...
Any help is very appreciated!
Thank you.
spring-config.xml file should be in src/main/resources. XML files in the Java source directory won't be included on the classpath.
This is automatic if you use the jetty plugin and run with mvn jetty:run (or jetty:run-war).
Your CLASSPATH doesn't include the Spring context.
I'd advise you to package your app into a WAR and deploy that to Jetty. WEB-INF/classes is always in the CLASSPATH, so if you copy your Spring context XML to that directory the class loader will find them.
Do you need a ContextLoaderListener in your web.xml?
I see applicationContext.xml mentioned in your web.xml, but not spring-config.xml. That's the one the class loader is complaining about.

Resources