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.
Related
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.
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.
Here is my modules.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/../...iml" filepath="$PROJECT_DIR$/../...iml" />
<module fileurl="file://$PROJECT_DIR$/7Wonders.iml" filepath="$PROJECT_DIR$/7Wonders.iml" />
<module fileurl="file://$PROJECT_DIR$/../AFLib/AFLib.iml" filepath="$PROJECT_DIR$/../AFLib/AFLib.iml" />
<module fileurl="file://$USER_HOME$/Documents.iml" filepath="$USER_HOME$/Documents.iml" />
<module fileurl="file://$USER_HOME$/Documents.iml" filepath="$USER_HOME$/Documents.iml" />
<module fileurl="file://$USER_HOME$/Documents.iml" filepath="$USER_HOME$/Documents.iml" />
<module fileurl="file://$PROJECT_DIR$/../MultiConnect/MultiConnect.iml" filepath="$PROJECT_DIR$/../MultiConnect/MultiConnect.iml" />
<module fileurl="file://$PROJECT_DIR$/../MultiConnect/lib/MultiConnect-lib.iml" filepath="$PROJECT_DIR$/../MultiConnect/lib/MultiConnect-lib.iml" />
<module fileurl="file://$PROJECT_DIR$/_7Wonders/_7Wonders.iml" filepath="$PROJECT_DIR$/_7Wonders/_7Wonders.iml" />
<module fileurl="file://$PROJECT_DIR$/../AFLib/lib/lib.iml" filepath="$PROJECT_DIR$/../AFLib/lib/lib.iml" />
</modules>
</component>
</project>
"7 Wonders" is the app, "MultiConnect" and "AFLib" are my libraries.
The stuff starting with $USER_HOME is completely unneeded: there is no Documents.iml file. I delete these lines, Android Studio adds them back. Then it complains that it can't open a module... 2 modules... 3 modules... and so on, until I delete those lines manually again. If I click on "Details" in the error message, it offers to remove those modules from the project, but that doesn't seem to have any effect. I even tried to make the modules.xml file read-only, but Android Studio complains and refuses to work.
This issue does not affect usability, but seeing that "Error Loading Project" every single time is rather annoying.
I don't need the ...iml file (the 1st line) either, but at least the Studio doesn't complain about it, and doesn't add several copies of it.
Can anyone explain what's going on?
There could be many reasons why this is happening.
1. Uninstall plugin "Markdown support" from plugins in settings.
Try compiling the whole project before gradle refresh is done. And set gradle settings as "Offline Work".
This will save a lot of time and will also save on re-installation of gradle dependencies. This re-install probably will cause refresh in modules.xml
I thought I wouldn't need to ask this but I am not having any progress.
The solution to this question:
How are maven scopes mapped to ivy configurations by ivy actually addresses question but in its theoretical part.
I have this configuration:
<conf name="compile" description="???" />
<conf name="runtime" description="???" extends="compile" />
<conf name="test" description="???" extends="runtime" />
<conf name="provided" description="???" />
Assume I have this dependency:
<dependency org="org.apache.tomcat" name="servlet-api" rev="6.0.16" transitive="false" />
What I want is: when I invoke the ivy:retrieve to copy the libraries to the .war lib directory before bundling it, I want only to copy all runtime (and compile implicitly) but no servlet-api.
so how to use ivy:retrieve then?
<ivy:retrieve conf="WHAT_TO_PUT_HERE" />
and how to configure the dependency:
<dependency conf="WHAT_IS_THE_CONF_MAPPING" org="org.apache.tomcat" name="servlet-api" rev="6.0.16" transitive="false" />
I'm plateauing here, so please any help would be appreciated.
Knowing that the ivy.xml for servlet-api defines the artifact with
conf="master"
So I think the question is how to 'really' map Provided scope of maven to the provided configuration of IVY.
This is how you map a dependency onto the local "provided" configuration:
<dependency org="org.apache.tomcat" name="servlet-api" rev="6.0.16" conf="provided->master"/>
The configuration mapping works as follows:
provided->master
^ ^
| |
Local Remote
config config
As explained in the answer the special "master" configuration contains only the artifact published by this module itself, with no transitive dependencies:
How are maven scopes mapped to ivy configurations by ivy
This means the "transitive=false" attribute is not required.
Update
How you use the configuration is up to you. The first option is simpler, but I prefer the second approach because my configuration reports match my classpath contents
Option 1
You can create a single classpath as follows:
<ivy:cachepath pathid="compile.path" conf="compile,provided"/>
This can then be used in the javac task as follows:
<javac ... classpathref="compile.path">
..
Option 2
Or I prefer to have a one-2-one mapping between configurations and classpaths:
<ivy:cachepath pathid="compile.path" conf="compile"/>
<ivy:cachepath pathid="provide.path" conf="provided"/>
The problem with the latter approach is that the javac task need to have the classpath usage explicitly stated as follows:
<javac ...
<classpath>
<path refid="compile.path"/>
<path refid="provided.path"/>
</classpath>
I think this explicitly explains how you use this special provided scope, but it's really up to you.
I've been going through the Ivy documentation and I have a question about the default ivysettings.xml found inside the ivy.jar.
All I want to do is change the public repository to a local Maven repository we have. That's it. I could copy all of the ivysettings*.xml files into my project, and use <ivy:settings> to point to it, but that duplicates a lot of stuff. I could also modify the ivy.jar, but that adds maintenance headaches. Developers have to use my ivy.jar, and if we go to a new version, I'd have to modify it again.
So, how do I keep all of the standard Ivy settings and simply switch the repository to use? I simply want to overlay my changes onto what Ivy already has.
And two more questions:
What's the difference between the ivyconf*.xml files and the ivysettings*.xml files? Why are there duplicate configurations in Ivy?
What's a good book on Ivy? I'm right now using Manning's Ant in Action which covers Ivy in a somewhat summary way and is a bit dated. The resources on the Ivy website itself are awful.
That's my ivysettings.xml file
<ivysettings>
<include url="${ivy.default.settings.dir}/ivysettings.xml"/>
<resolvers>
<chain name="download-chain" changingPattern=".*" checkmodified="true" >
<ibiblio name="maven" m2compatible="true" />
</chain>
</resolvers>
</ivysettings>
Notice that I write my extra resolvers here, but use everything else from the standard one, which is indicated in the url. This will loads the settings from the ivysettings.xml in the ivy.jar file.
As to the ivyconf*.xml. I think it is deprecated now. Ivysettings is the new way of doing this.
The resources are pretty awful. I totally concur to that. However, lots of answers in the stackoverflow.com were verbose enough and actually try to anticipate problems
Mark O'Connor's answers are particularly verbose and straight to the point. You have to embrace the fact that you are learning something new, just give it time.
Finally figured it out.
I copied the ivysettings.xml file from the jar and made a slight modification. Note that the first include points to an XML file in ivy ${ivy.lib.dir} and not to ${ivy.default.settings.dir}:
<ivysettings>
<settings defaultResolver="default"/>
<include file="${ivy.lib.dir}/ivysettings-public.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-shared.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-local.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-main-chain.xml"/>
<include url="${ivy.default.settings.dir}/ivysettings-default-chain.xml"/>
</ivysettings>
I have my own ivysettings-public.xml which is the same as the default, but now defines a root to my repository. (Yes, it's localhost for now, but I'll set it to an actual server once I get everything resolved):
<ivysettings>
<resolvers>
<ibiblio name="public" m2compatible="true"
root="http://localhost:8081/artifactory/repo" />
</resolvers>
</ivysettings>
Now, in my build.xml, I have the following:
<property name="ivy.lib.dir" value="${basedir}/ivy.lib"/>
<taskdef uri="ivylib:org.apache.ivy.ant"
resource="org/apache/ivy/ant/antlib.xml">
<classpath>
<fileset dir="${ivy.lib.dir}">
<include name="ivy.jar"/>
<include name="ivy-*.jar"/>
</fileset>
</classpath>
</taskdef>
<ivy:configure file="${ivy.lib.dir}/ivysettings.xml" override="true"/>
That seems to do the trick.
In your build.xml include following:
<property name="ivy.settings.dir" value="PATH OF SETTINGS" />
<property file="${ivy.settings.dir}/ivysettings.properties" />
<ivy:settings file="${ivy.settings.dir}/ivysettings.xml" />