How can I exclude generated code files from PMD? - java

I try to build and check a java project in IntelliJ with Gradle, but my project has some generated code. So I tried to exclude the code from the checkstyle and the PMD. With checkstyle everything is fine, but the PMD does strange things.
The generated code folder structure is:
generated_functions
+--coe-caller
+--+--calls
+--+--methods
+--+--structs
Coe-caller, calls, methods and structs contain .java files.
For PMD it should be possible to exclude classes or packages:
pmdMain {
excludes = [
'**coe_caller**'
]
}
The thing is that it works, if I use only one subfolder(calls,methods,structs), which contains the java files. If I add a second folder to the list, it doesn't work again.
Could someone explain the behaviour and give me a solution for it?
The alternative way for me is to separate the generated code to its own module and I deactivate the checks for it, but I think it should not be the final solution.
configure(allprojects.findAll{!it.name.equals('generated_rpc_functions')}){
apply plugin: 'jacoco'
apply plugin: 'pmd'
apply plugin: 'checkstyle'
checkstyle{ ... }
Has someone another idea how generated code can be exluded in gradle?

Related

problem in Gradle adding dependencies through commandline

I'm trying to add a bunch of dependencies through the commandline. This is because my workplace has a bunch of plugins that we want to preload into every project. and we dont want devs to mess about their gradle files.
So I looked at this older issue - https://stackoverflow.com/a/40613776/112050 and tried to do the same in this repo https://github.com/arakoodev/bug-gradle
however, I'm constantly getting a "cannot find symbol" error:
error screenshot - https://github.com/arakoodev/bug-gradle/blob/main/error%20msg.png:
I'm only using an empty springboot application, but moved the dependency declaration from build.gradle into myinit-script.gradle.
The code can't find the classes from the springboot dependency.
It seems you're mixing a few patterns...
If you want to include org.springframework.boot:spring-boot-starter-web:3.0.0 as a dependency, and not merely add it as a classpath, using classpath wont accomplish this.
I did verify that if you change your myinit-script.gradle to the following, it will work:
allprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:3.0.0'
}
}
What's going on here?
Changed initscript to allprojects to apply it to all projects.
Perhaps you could still use initscript, but I think the -I is accomplishing this. Anyway, followed the pattern from here:
https://docs.gradle.org/current/userguide/init_scripts.html
You cant include dependencies unless you have already applied the java plugin, so that was added.
classpath doesn't include a dependency, implementation does, so changed that.
Now, when I run this, gradle -I .\myinit-script.gradle clean build, I get a clean compile.

Including javax persistence in a Cordova/Android project

I am trying to incorporate ObjectBox in my hybrid Cordova/Android project. By dint of some trial and error I have managed to figure out two of the steps involved.
The app level build.gradle file has to be modified to include the ObjectBox Gradle plugin classpath "io.objectbox:objectbox-gradle-plugin:2.5.0"
Define a build-extras.gradle file to "apply" the ObjectBox plugin ext.postBuildExtras = {apply plugin: 'io.objectbox'}
The next step according to the ObjectBox docs is to define at least one Entity class
However, the issue here is that I need to import the javax.persistence.* classes into the project. It is not clear to me how I do this. I have run into suggestions along the lines of including
compile group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
in the dependencies section of the app level build.gradle file. However, this causes gradle to complain that it does not know the compile() function. I'd be much obliged to anyone who might be able to tell me how this should be done.
For the benefit of anyone running into this thread - you can download the JAR file for javax.persistence here. Place this line in the folder src/android/libsof your custom plugin and then modify plugin.xmlwith the line
<lib-file src='src/android/libs/name-of-javax-persistence.jar'/>

Accessing classes from separate modules

I have a project with the following basic setup:
Root Project
- employee-service
- nomination-service
I need to access classes from employee-service in nomination-service, but am not able to get the dependencies down for some reason.
Here's the root project's settings.gradle:
rootProject.name = 'pair-project'
include 'applications/employee-service'
include 'applications/nomination-service'
Inside of the nomination-service build.gradle I have tried all the following (each dependency denotes one thing I've tried, they're not all present at the same time):
dependencies {
project(':employee-service')
project(':../../applications/employee-service')
compile(':../employee-service')
compile(':employee-service')
compile('./applications/employee-service')
compile('/employee-service')
}
All of these doesn't work for some reason or the other.The project ones generally tell me it can't find a project at that location. When I use compile it will usually build fine, but then can't find the import for the class I'm trying to use.
Any help would be greatly appreciated here, everything I've read online has generally suggested taking this approach, so I'm not sure what I'm doing wrong.
The applications/nominations-service/build.gradle should be:
apply plugin: 'java'
dependencies {
compile project(':applications/employee-service')
}
Generally, from here dependencies require a configuration and one or more items with a dependency notation. In this case, compile is the configuration and we are using a project dependency. All of your examples only had one, but not both.

What is minimal sample Gradle project for ANTLR4 (with antlr plugin)?

I have created new Gradle project, added
apply plugin: 'antlr'
and
dependencies {
antlr "org.antlr:antlr4:4.5.3"
to build.gradle.
Created src/main/antlr/test.g4 file with the following content
grammar test;
r : 'hello' ID;
ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;
But it doesn't work. No java source files generated (and no error occurred).
What I missed?
Project is here: https://github.com/dims12/AntlrGradlePluginTest2
UPDATE
I found my sample is actually works, but it put code into \build\generated-src which I was not expecting :shame:
I will add onto other answers here.
Issue 1: Generated source files are placed in build/generated-src folder.
I found this discussion, but the solution there (setting outputDirectory property) is a bad idea. If you do gradle clean build command, this will clear out your entire source directory. The discussion there gives a good explanation as to why you should not
the antlr generated sources are generated into a
subdirectory of the "build" folder like all other artifacts, which are
generated during the build. Furthermore your generated directory
projectRoot/build/generated-src/antlr/main is added to the java
sourceset definition to be sure its considered compileJava task.
If you write the antlr generated source directly to the src/main/java
folder you're polluting your source folder with output of your build
process. ... Polluting your source folder during your build is an
antipattern I think.
However, if you want to do this, you can add a gradle task to copy the generated files to the build directory.
generateGrammarSource << {
println "Copying generated grammar lexer/parser files to main directory."
copy {
from "${buildDir}/generated-src/antlr/main"
into "src/main/java"
}
}
Issue 2: Generated source files do not have package attribute set.
To solve this issue, add something like the following near the top of the grammar file:
#header {
package com.example.my.package;
}
What helped me is two things:
Add header:#header{ package com.example.something.antlrparser; } to the grammar file directly after the grammar test; declaration.
Place the grammar file in corresponding folder, i.e. src/main/antlr/com/example/something/antlrparser/grammar.g4
Now when I run the generateGrammarSource gradle task, .java files are generated in /build/generated-src/antlr/main/com/example/something/antlrparser/*.java and they are automatically picked up by IntelliJ as well as compilable by gradle.
The build.gradle file is just:
group 'com.example.something'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'antlr'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
antlr "org.antlr:antlr4:4.5" // use ANTLR version 4
}
Add this to your build.gradle
generateGrammarSource {
outputDirectory = file("src/main/java/com/example/parser")
}
add this to your grammar after your "grammar ";
#header {
package com.example.parser;
}
Tested and working with Java8 grammar from antlr example grammars
Additional Link(s):
Here is a short guide of the Antlr plugin from docs.gradle.org
For Issue 2:
you can configure in the gradle.build:
generateGrammarSource {
maxHeapSize = "64m"
arguments += ["-visitor",
"-long-messages",
"-package", "your.package.name"]
}
A snippet is included in the Gradle "all" distribution under the "snippets" folder. You can also simply browse the snippet on GitHub.
https://github.com/gradle/gradle/tree/master/subprojects/docs/src/snippets/antlr
The Gradle sample noted by #Mark Vieira only got me halfway there. I found that I had to specify the package in the header of my ANTLR grammar file in order for everything to be seen in both directions (generated code able to access hand-written code and vice-versa).
grammar MyGrammar;
#header {
package com.mypackage;
}
Prior to switching to Gradle, I had been using the ANTLR plugin in IntelliJ, which filled in the package for me. Upon switching to Gradle, the package went away, which caused problems.
Source: https://stackoverflow.com/a/1655920/1877143
Gradle STS plugin doesn't generate source files for antlr4. It generates the misleading output as:
[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks:
[sts] generateGrammarSource
[sts] -----------------------------------------------------
:generateGrammarSource UP-TO-DATE
Uninstalled this old plugin and used from command line..It works !

android gradle -- compile two pure java projects to dex

I have two pure java projects (:java:libs:proj1, :java:libs:proj2) that produce two classes.jars when they are built. The two jars "combined" include the exact classes that I would like to convert into dex jar i.e. a JAR file with classes.dex in it.
Can someone help me with converting the two pure java projects into a single JAR with classes.dex in it?
Here is what I tried doing.
1) Create a separate Android Library project (apply plugin: 'android-library') and include the two java projects as dependencies.
apply plugin: 'android-library'
dependencies {
compile project(':java:libs:proj1')
compile project(':java:libs:proj2')
}
However, with this approach, it doesn't generate a dex file. Instead, it produces an ".aar" file which contains only the .class files of the two projects. If I can convert this .aar file to its dex, then this would be perfect but apparently the "android-library" plugin does not have a "dex" task.
2) Create an Android project (apply plugin: 'android') and include the two projects as depenendencies. (Similar to above but using 'android' instead of 'android-library').
apply plugin: 'android'
dependencies {
compile project(':java:libs:proj1')
compile project(':java:libs:proj2')
}
This approach does create the dex files that I need, however it also includes ALL the dependencies for my two pure java projects. So for example, my java projects depend on JUnit, BouncyCastle and some other external JAva projects. Inside of the classes.dex, I see that it has included everything. Is there a way to tell the "dex" task to not include dependencies "transitively"?
I only want the classes directly associated with my two java projects into dex. I wish there was an easy way :(
Thanks,
J
will modify compile in dependencies to provided help? please also check this
25.4.7. Excluding transitive dependencies
You can exclude a transitive dependency either by configuration or by dependency:
Example 25.14. Excluding transitive dependencies

Categories

Resources