How to Configure Eclipse to Work with `ehcache` - spring

Summary/Question
I've rolled onto a project that uses ehcache. The project is maven enabled and when I run mvn clean install from cli all ends well.
The project can also be opened in NetBeans and everything displays properly, however when I open the project in eclipse, I'm seeing some errors related to ehcache. In NetBeans the spring-cache.xml is located under a "Web Pages" folder. This made me think that the eclipse project may not be recognized as a dynamic web project, however following the directions here: https://www.mkyong.com/java/how-to-convert-java-project-to-web-project-in-eclipse/ I was able to verify that the project is indeed setup to support Dynamic Web Module, Java and JavaScript.
I also opened the project in IntelliJ and also did not see any issues with the spring-cache.xml.
I also didn't find documentation related to this version of ehcache on http://www.ehcache.org/documentation/ but the fact that things work in NetBeans, IntelliJ and via mvn clean install make me think its my eclipse setup. Thoughts?
More Details:
The errors I see in eclipse are:
Multiple annotations found at this line:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'ehcache:annotation-driven'.
- schema_reference.4: Failed to read schema document 'http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring-1.1.xsd, because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
And
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'ehcache:config'.
My maven dependency looks like:
<dependency>
<groupId>com.googlecode.ehcache-spring-annotations</groupId>
<artifactId>ehcache-spring-annotations</artifactId>
<version>1.2.0</version>
</dependency>
Which matches what it should be, based on: https://mvnrepository.com/artifact/com.googlecode.ehcache-spring-annotations/ehcache-spring-annotations/1.2.0
My spring-cache.xml file 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:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<ehcache:annotation-driven />
<ehcache:config cache-manager="cacheManager">
<ehcache:evict-expired-elements interval="60" />
</ehcache:config>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="${ehcache.config.location}"/>
</bean>
</beans>

Looks like current versions of NetBeans and IntelliJ aren't as strict on checking things as eclipse is. :) The following is what I found out.
From ehcache with Spring. google code xsd file not found I found out that the top error was related to the fact that the xsd is no longer hosted at the url provided.
Doing some googles, I found a reference to the version on github at: https://raw.githubusercontent.com/agentgt/ehcache-spring-annotations/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.2.xsd by changing my bottom xsi schemalocation to the above url, it resolved the issue in eclipse.
The stack overflow questions referenced, also provides a link to the google code archive of: https://code.google.com/archive/p/ehcache-spring-annotations/source/default/source
If you download the zip, you can then find the offical xsd in the zip at: /schema/ehcache-spring/ehcache-spring-1.2.xsd (it also has ehcache-spring-1.0.xsd and ehcache-spring1.1.xsd in case anyone else has issues with even earlier versions).
I found a way to reference the local schema file here: How to reference a local XML Schema file correctly?
I'm going to post a question on how to reference it from a relative path of the project, as we have both windows and mac developers on this project. If this is of use to you, the details will be here: How to Reference Local XSD File By Relative Path

Summary of issue:
xsi-schemaLocation reference:
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
results in validation-issues in eclipse, on commandline maven there are no issues.
replacing the ref with:
https://raw.githubusercontent.com/agentgt/ehcache-spring-annotations/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.1.xsd
solves issue in eclipse. In maven the saxparser will complain however if you 're not connected to internet.
By putting a proxy ref :
-Dhttp.proxyHost=[proxyhost] -Dhttp.proxyPort=[proxyport] -Dhttps.proxyHost=[proxyhost] -Dhttps.proxyPort=[proxyport]
the maven build succeeds....
I investigated this difference in behaviour:
in the ehcache-spring jar, ehcache-spring-annotations-1.1.2.jar,
a META-INF/spring.schemas file is present.
Content of that file:
http\://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.0.xsd=com/googlecode/ehcache/annotations/ehcache-spring-1.0.xsd
http\://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd=com/googlecode/ehcache/annotations/ehcache-spring-1.1.xsd
cmdline maven uses this content (and finds the xsd in the jar).
eclipse does not.
Solution I used:
go to eclipse preferences,
XML -> XML Catalog and add a new user entry:
Location: https://raw.githubusercontent.com/agentgt/ehcache-spring-annotations/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.1.xsd
Key type: Public id
Key: http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
Alternative web adress: https://raw.githubusercontent.com/agentgt/ehcache-spring-annotations/master/core/src/main/resources/com/googlecode/ehcache/annotations/ehcache-spring-1.1.xsd
Re-validating spring context files solved issues in my eclipse workspace.
I like this solution better because it is a shortcoming of eclipse which i solve with an eclipse-specific solution.

Related

Quarkus xml Parser DocumentBuilderFactory cannot be found, but only when using quarkus-run.jar

When packaging our app with mvn package everything works fine. Then when we start our app with java -jar target\quarkus-app\quarkus-run.jar the app silently crashes. While debugging we found that it crashes while parsing an xml InputStream. It happens while initialising some classes.
This is the stacktrace that we had to dig out ourselves:
Exception occurred in target VM: Provider for javax.xml.parsers.DocumentBuilderFactory cannot be found
javax.xml.parsers.FactoryConfigurationError: Provider for javax.xml.parsers.DocumentBuilderFactory cannot be found
at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source)
at org.optaplanner.core.impl.io.jaxb.GenericJaxbIO.parseXml(GenericJaxbIO.java:209)
at org.optaplanner.core.impl.io.jaxb.SolverConfigIO.read(SolverConfigIO.java:15)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlReader(SolverConfig.java:199)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlInputStream(SolverConfig.java:173)
at org.optaplanner.core.config.solver.SolverConfig.createFromXmlInputStream(SolverConfig.java:160)
When packaging the app in an uberjar this problem does not occur. Same when using dev.
We use graalvm-ce-java17-22.2.0, together with the 2.11.2.Final version of quarkus and the 8.29.0.Final version of optaplanner.
We tried to verify that there aren't any xml exclusion in the dependencies. Also we checked if quarkus and the quarkus maven-compiler-plugin are of the same version. Also we looked into the compiled jarfiles, if the xml we want to read is present. If it wouldn't be present, the code would crash even earlier. The class javax.xml.parsers.DocumentBuilderFactory is not listed in the quarkus-app-dependencies.txt
Adding the quarkus-optaplanner extension helped to identify the logger issue. So the problem with the silent crash is resolved. Adding quarkus-jaxp to the dependencies gets rid of the FactoryConfigurationError and everything works as expected.

Specifying files by absolute path was removed in Liquibase 4.0

I am getting the following liquibase error when I run my Spring Boot application:
Specifying files by absolute path was removed in Liquibase 4.0. Please use a relative path or add '/' to the classpath parameter.
Here is the class path in application.yaml:
liquibase:
change-log: classpath:db/changelog/db-changelog-master.xml
I also tried:
liquibase:
change-log: classpath:/db/changelog/db-changelog-master.xml
Here is folder structure:
Changlog master:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<include file="db-changelog-1.0.xml"/>
</databaseChangeLog>
I got this issue when putting the changelog files outside the resources folder, but if I include them under resources/db/changelog, then it would work fine with setting the bellow config.
spring.liquibase.change-log=classpath:/db/changelog/changelog-master.xml
Tested under 4.6.2
This is still an open issue unfortunately. See 2281.
Looks like this was fixed in v4.4.3
As explained here
How the Liquibase classpath worked before version 4.0
Before version 4.0, one of the default locations Liquibase added to
the classpath was the root directory in your filesystem (/). The
change caused issues because of a machine-dependent changelog path,
such as /home/my-user/projects/liquibase/changelog.xml, found under
the / directory. This way, Liquibase uses the given path as part of
the changeset identifier stored in the DATABASECHANGELOG table, and
when you run Liquibase from
/home/other-user/projects/liquibase/changelog.xml, Liquibase sees it
as a different changelog and tries to rerun all the previously run
changesets.
To prevent identification issues from happening, a / was removed as a
default part of the classpath. How the Liquibase classpath works in
4.0 and later versions
Starting with Liquibase 4.0, the root directory (/) is no longer a
default part of the classpath because of the issue mentioned in the
previous section.
...
The message "Please use a relative path or add '/' to the classpath parameter." refers to the root directory '/', and does not mean to add a slash to the start of your classpath path. Afaik, classpath:x and classpath:/x are the same.
Also, the message appears when the master changelog is not found, for whatever reason, so also a typo can cause this message. It's only a hint telling you that it might not be found because the file is not on the classpath, because they removed the root directory from the classpath, but it could also not be found because you specified the wrong path (I just did that).
To configure it correctly, the master changelog must be on the Liquibase classpath. In Spring Boot, the Liquibase classpath is set to the application's classpath, i.e. you can use src/main/resources.
Tl;dr: When your file is src/main/resources/db/changelog/db.changelog-master.xml use
spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml
I don't know, whether there was or is a bug in regards to that with certain Liquibase versions, but that's how it's supposed to work, anyway.
Just try to remove "classpath:" from "change-log:" parameter.
Also try to check your pom.xml ("changeLogFile" tag in configuration):
there should not be a "${basedir}" before change log file path.
<changeLogFile>
/src/main/resources/liquibase/changelog.xml
</changeLogFile>

Adding Maven Dependencies to Ant

I'm trying to implement adding maven dependencies to my ant build using this article. I beleive I have everything set up as indicated by the article but since it is not working I must be missing something. I am getting this error:
Could not load definitions from resource org/apache/maven/artifact/ant/antlib.xml. It could not be found.
My searches for this error seem to imply I an missing maven-ant-tasks-2.1.3.jar. However, it is in the path indicated by the classpath. Can someone help be debug this? TIA.
I found at least part of the problem. I had the wrong file name in the classpath. It was maven-ant-tasks.jar and it should have been maven-ant-tasks-2.1.3.jar.
Unfortunately I now have a different problem which I think implies a corrupt jar file.
Unable to obtain resource from /usr/build/lib/maven-ant-tasks-2.1.3.jar: java.util.zip.ZipException: error in opening zip file
Downloading the jar file again did not help.
Never mind. Apparently the Apache maven web site has a corrupt version. I downloaded it from the mvnrepository and that worked.

uglify-maven-plugin adding exclude file list

I am trying to implement uglifyjs-maven-plugin.
While using the configuration mentioned here, I am getting error like sourceDirectory is undefined or invalid.
Using the configuration like
<configuration>
<sourceDirectory>target/snapshot/javascript</sourceDirectory>
<outputDirectory>target/snapshot/javascript</outputDirectory>
</configuration>
its working fine. But how will I add exclude file list in the above configuration.
I have a js file of 900kb, when i am including that js file inside source directory, i am getting exception during maven build as " Exception in thread "main" java.lang.StackOverflowError". Can anyone help me out of this.
It looks like that while the documentation (and probably the current implementation) supports file exclusions, the version available in the repository (1.0) is older and supports only sourceDirectory.
I have not found any newer version published anywhere, so I guess the best thing we can do is compile the latest version of the plugin manually and see if it supports what the documentation says.

Netbeans Including XML and Properties when building Spring projects with Maven

I am new to Maven and Spring. I'm using Netbeans 7 as my IDE, and setting up a Spring 3 project using Maven.
Everything seemed to set up smoothly, and I began running through the Spring User Guide. However, I'm getting a file not found exception when trying to load my context.xml file.
I have an App class located at com.myproject and the context.xml file is located at com.myproject.conf
I'm using the following line of code in App.java to try and load the context.xml file:
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
But when I run the application, it results in:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [context.xml]; nested exception is java.io.FileNotFoundException: class path resource [context.xml] cannot be opened because it does not exist
Looking at the NetBeans output, it also looks like it's not picking up the log4j.properties file which is also located in com.myproject.conf
I looked at the jar that the build process created, and the entire com.myproject.conf package is missing, meaning the .xml and .properties are missing as well. I've tried moving these config files to the com.myproject package as well as just putting them at the root of the project which don't yield any different results.
So I'm making the assumption that my maven project isn't set up entirely correctly, or maybe a setting isn't correct within NetBeans.
It seems like you need to learn about resources in Maven projects (such as XML, bitmaps, etc...). These are stored in separate directories. See here.
Put the package in "Other Sources"
I have the same problem with a .property file to manage the Internationalization.
I create it in com.company.app.view.resources at the Source Packages directory.
When I build the project and then look at my war file (target).. I don't found the .property file in /WEB-INF/classes/com/company/app/view/resources
Then I put the package in "Other Sources" directory and it works for me.

Resources