Does anybody know how to check sql files for custom checkstyle rules. For example:
ALTER TABLE MYLOCALSCHEME.MYTABLE...
should be
ALTER TABLE MYTABLE...
without schema name,
because there are different schemes in different environment.
check sql files for custom checkstyle rules
Unless you want to make your own antlr parser for the language, you can use RegexpSingleline to create some basic rules using regular expression.
Example Config:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="fileExtensions" value="sql"/>
<module name="RegexpSingleline">
<property name="format" value="ALTER TABLE \w+\.\w+"/>
<property name="minimum" value="0"/>
<property name="maximum" value="0"/>
</module>
</module>
A custom parser would involve a new g4 Lexer and Parser, a new class that iterates through the nodes (like TreeWalker), an AST class (like DetailAST), a base class for all new checks (AbstractCheck), and then any and all new Checks you want.
Related
I am trying to apply a persistence framework to an eclipse rcp e4 application. For this purpose, I created an example of a feature project, almost following the tutorial https://www.vogella.com/tutorials/JavaPersistenceAPI/article.html, but adapting for rcp. However I got the error "Persistence provider for EntityManager named xxx not found". I have searched for a week but found no solution. Could someone please tell me, what the error really is and how to resolve it?
The persistence.xml is located in project tasks.model folder META-INF. The eclipse.jar and javax.persistence.jar are in another plugin project (Earlier I defined maven dependencies in pom.xml. Although eclipse reports no compilation errors, but running rcp product would result in an error that ClassDefNotFoundException for persistence, so putting javax.persistence.jar in a separate plugin removed this error). Some answers suggested that the persistence.xml should be in the folder src/resources/META-INF. But in a plugin project the folder META-INF is already in its default place. I even created an extra folder resources/META-INF and put the persistence.xml in that, but it did not help. If I created a normal maven project, then things work properly, also when I change the database to mysql in localhost. The errors appear when I convert the project into a plugin project for building rcp applications.
The error "No Persistence provider for EntityManager" occurs when I invoke the command:
factory = Persistence.createEntityManagerFactory("todos");
My persistence.xml file looks like:
<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="todos" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>tasks.model.Todo</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:/tempGeo/simpleDB" />
<property name="javax.persistence.jdbc.user" value="test" />
<property name="javax.persistence.jdbc.password" value="test" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
</properties>
</persistence-unit>
</persistence>
Eclipse 2018-12
Eclipse Checkstyle Plug-in 8.12.0
Java jdk11.0.1
In my eclipse projects, i use Google’s auto-value. Files are autogenerated in /projectRoot/.apt_generated/tld/domain/some/package/AutoValue_AnyName.java. I want to exclude those files from checkstyle checks, so i added
<module name="BeforeExecutionExclusionFileFilter">
<property name="fileNamePattern" value=".*/.apt_generated/.*/AutoValue_.*"/>
</module>
in my checkstyle.xml. IMO, that should match the autogenerated files and exclude them from scanning. Alas, Eclipse’s opinion differs. The files are scanned, and Google does not follow my coding style, so i get a lot of “Problems”. How can i exclude those files from checkstyle?
You can exclude from checking by adding this to your project's .checkstyle file:
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
...
<filter name="DerivedFiles" enabled="true" />
</fileset-config>
This assumes that AutoValue/APT is marking the generated files as derived.
Or if your generated types are restricted to a particular folder/package:
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
...
<filter name="FilesFromPackage" enabled="true">
<filter-data value="derived" />
</filter>
</fileset-config>
Or you can try:
<module name="SuppressionFilter">
<property name="file" value="${config_loc}/.checkstyleSuppress.xml" />
</module>
with an appropriate supress xml file. It is very versatile but takes some work to set up for the first time.
I am using the Checkstyle plugin in IDEA. I want to set up different checkstyle configurations to my different modules. I am using gradle as build tool-version 4- and I want to write a task that modifies the corresponding .iml files of the modules. Any idea how to do that?
My very first attempt in modifying the iml file looking over here
apply plugin: 'idea'
task setCheckStylePluginSettings {
group = "Idea"
description = "Copies CheckStyle plugin settings to Idea workspace."
println "Step 1."
idea.module.iml {
withXml {xmlProvider ->
// Get root node.
println "Step 2."
def project = xmlProvider.asNode()
}
}
}
However, I am stuck just at the beginning that I cant event see the Step 2 printed on the Console.
A "module" in IntelliJ is a one-to-one mapping to a SourceSet in Gradle, assuming you imported the project with the "Create separate modules per source set" option checked.
By default, the Checkstyle plugin adds tasks for each source set that is added to the build. So, you should already have the tasks checkstyleMain and checkstyleTest when you apply the java plugin. These tasks are effectively what you're looking for.
Now, to customize them, in your build.gradle, configure them like so:
checkstyleMain {
configFile = file("${rootDir}/checkstyle/main.xml")
}
checkstyleTest {
configFile = file("${rootDir}/checkstyle/test.xml")
}
This assumes that you have different Checkstyle configuration files in your project at ${rootDir}/checkstyle/.
So the problem is solved. I have tried the solution proposed by Thomas Jansen in this question.
But I will give more information on how to do it.
In order to give different checkstyle modules to different sourcesets you need to define id tag in the module. Shown below:
<module name="ConstantName">
<property name="id" value="ConstantNameMain"/>
<property name="severity" value="error"/>
<property name="applyToPrivate" value="false"/>
<property name="format" value="^[A-Z][A-Za-z0-9]*(_[A-Za-z0-9]+)*$"/>
</module>
<module name="ConstantName">
<property name="id" value="ConstantNameTest"/>
<property name="severity" value="error"/>
<property name="applyToPrivate" value="false"/>
<property name="format" value="^[A-Z][A-Za-z0-9]*(_[A-Z0-9]+)*$"/>
</module>
Then we define SuppressionFilter module for suppression.xml which can be located at the same folder with your checkstyle.xml. One important thing is to locate the SuppressionFilter module as Checker module.
<module name="Checker">
<property name="severity" value="warning"/>
<module name="SuppressionFilter">
<property name="file" value="./suppressions.xml"/>
</module>
<module name="TreeWalker">
.
.
.
</module>
</module>
Then, we define the suppression.xml file as below:
<suppressions>
<!-- >Test sources suppressions</!-->
<suppress files="[\\/]src[\\/]test[\\/].*" id="ConstantNameMain" />
<!-- >Main sources suppressions</!-->
<suppress files="[\\/]src[\\/]main[\\/].*" id="ConstantNameTest" />
</suppressions>
Aaaaaand lastly, configure your Checkstyle-IDEA plugin, activate real time scan from Settings>Editor>Inspections>Checkstyle and you are done.
While deploying my jpa project to wildfly server i am gettings following warning:
The collection of metamodel types is empty.
Model classes may not have been found during entity
search for Java SE and some Java EE container managed persistence units.
Please verify that your entity classes are referenced in persistence.xml using
either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
My persistence xml looks like this
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="primary" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/example</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
<property name="eclipselink.target-server" value="JBoss"/>
<property name="eclipselink.weaving" value="static"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/example"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="xxx"/>
</properties>
</persistence-unit>
</persistence>
My Database tables are not created due to this.
Please help me understand this problem. As i am not able to understand the root cause of this.
For people who would come across this question like I did: I've just met this issue in Eclipse 2020-06 when running JPA Tools > Generate Tables from Entities, which is a situation probably different from the one in the question albeit with a strictly identical error message.
It turns out that the only thing needed to work around it is to hit Project > Clean and select the JPA project in the list.
For some unknown reason in my case, cleaning the project is necessary between runs of the Generate Tables command. I can't figure out why but that's the way it is. HTH
My persistence.xml is currently present in the application war(in META-INF folder.). However, for the application to be run across multiple DBs the persistence needs to be changed again and again. I want to avoid it. But, I am not able to understand how will i configure the properties(like dialect) in the persistence.xml from, say, a property file which i would change based on my DB, hence not compelling me to update and redeploy my war.
My problem can also be resolved if i can configure the dialect in the datasource in standalone.xml where i have mentioned other DB details. I am not being able to make out what the property would be.
Though i would prefer a solution for the first one.
PS: I am rookie in Web App development. Questions might annoy you. :D
I use a method that works well for hibernate.
1) put the hibernate configuration properties in a xml file (call it hibernate.cfg.xml but it's not mandatory)
this is an example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.search.default.directory_provider">ram</property>
</session-factory>
</hibernate-configuration>
you can put there only hibernate properties that do not start with hibernate.ejb
2) Create a jboss module. It's very simple. Suppose you want to call the module com.myorganization.config than create a directory structure in the modules folder of you server installation: /com/myorganization/config/main. In the main folder put the hibernate.cfg.xml file and the following module.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.myorganization.config">
<resources>
<resource-root path="."/>
</resources>
</module>
3) In your persistence.xml adding the following property:
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml" />
4) Finally, in the META-INF/MANIFEST.MF file add the following line:
Dependencies: com.myorganization.config
If you like maven use the maven-war-plugin in order to change the MANIFEST.MF:
<configuration>
<archive>
<manifestEntries>
<Dependencies>com.myorganization.config</Dependencies>
</manifestEntries>
</archive>
</configuration>
That's all.