How to set checkstyle configuration from Gradle in IDEA - intellij-idea

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.

Related

RCP e4 EclipseLink Error: No Persistence provider for EntityManager named

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>

Exclude path from checkstyle-plugin in eclipse

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.

maven checkstyle sql/plsql plugin

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.

How to externalize properties of persistence.xml for JBOSS 7.1.1?

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.

Adding folder to classpath in Velocity using Spring framework

How can I add an entire folder to Velocity and use the files inside as resource bundles?
Currently in my velocity.xml I have this code:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="parentMessageSource">
<ref bean="globMessageSource"/>
</property>
<property name="basenames">
<list>
<value>classpath:/WEB-INF/i18n</value>
<value>/WEB-INF/templates/velocity/my_vm_template</value>
</list>
</bean>
I need to add i18n folder to the classpath so that its containing files can be seen by the VelocityTools version 1.4 in the toolbox.xml:
<tool>
<key>test</key>
<scope>request</scope>
<class>org.apache.velocity.tools.generic.ResourceTool</class>
<parameter name="bundles" value="i18n.ss_messages"/>
<parameter name="locale" value="en_US"/>
</tool>
The code is giving me an error message: "java.util.MissingResourceException: Can't find bundle for base name i18n.ss_messages, locale en_US"
This question relates to VelocityTools error - "java.util.MissingResourceException: Can't find bundle for base name WEB-INF.conf.resources.ss_messages, locale en_US"
Sorry if it's a silly question, but I can't find anywhere describing how to add an entire folder to the classpath and be available as a bundle so it can support Velocity template localization.
IMPORTANT NOTE!! If I place my ss_messages_bg_BG.properties and ss_messages_en_US.properties files in /WEB-INF/classes/i18n then it works, but I want to place them in a different folder ideally in /WEB-INF/templates/i18n. How do I do that?
Do you use maven? You could tell maven to use that directory as a resource:
<resource>
<!-- Velocity requires to be in classpath -->
<directory>src/main/webapp/WEB-INF/i8n</directory>
<filtering>true</filtering>
</resource>