Checkstyle custom check does not work on gradle checkstyle plugin - java

Question
I have created own custom check for checkstyle and it works on commandline and via maven checkstyle plugin.
However via gradle checkstyle plugin, it occurs below error.
* What went wrong:
Execution failed for task ':my-project:checkstyleMain'.
> Unable to create Root Module: config {C:\Users\[path to my project]\build\tmp\resource\string8421659201972573805.txt}, classpath { ...many of classpathes. not "null" }.
Whenever exclude custom check from checkstyle.xml, the task works.
How to make custom check works on gradle?
Versions
checkstyle: 8.37
maven checkstyle plugin version: 3.1.2
gradle version: 5.6.2
Implementation
custom check
public class MyCheck extends AbstractCheck {
...
checkstyle.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://checkstyle.sourceforge.net/dtds/configuration_1_3.dtd">
<module name = "Checker">
...
<module name="TreeWalker">
...
<module name="package.to.my.check.MyCheck"/>
</module>
</module>
Custom check class and checkstyle.xml are packaged into an artifact named "mycheck-module".
pom.xml (It works)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.37</version>
</dependency>
<dependency>
<groupId>package.to.my.check.module</groupId>
<artifactId>mycheck-module</artifactId>
<version>[version]</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectories>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
</sourceDirectories>
<configLocation>checkstyle.xml</configLocation>
<enableFilesSummary>true</enableFilesSummary>
<maxAllowedViolations>0</maxAllowedViolations>
<violationSeverity>warning</violationSeverity>
<consoleOutput>true</consoleOutput>
<failOnViolation>
false
</failOnViolation>
<propertyExpansion>
checkstyleSuppressionConfigDir=${project.basedir}
</propertyExpansion>
</configuration>
</plugin>
build.gradle (It does not work)
buildscript {
...
dependencies {
...
classpath 'package.to.my.check.module:mycheck-module:[version]'
}
}
...
// Set up checkstyle
apply plugin: 'checkstyle'
def checkstyleSuppressionConfigDir = file("${rootDir}/suppressCheckstyle")
checkstyle {
toolVersion = '8.37'
sourceSets = [it.sourceSets.main]
config = resources.text.fromString(getClass().getResourceAsStream('checkstyle.xml').text)
ignoreFailures = false
maxWarnings = 0
maxErrors = 0
configProperties.checkstyleSuppressionConfigDir = checkstyleSuppressionConfigDir
}

I have resolved myself.
You can add your dependency into checkstyle with [Append] in below:
build.gradle
buildscript {
...
dependencies {
...
classpath 'package.to.my.check.module:mycheck-module:[version]'
}
}
...
// Set up checkstyle
apply plugin: 'checkstyle'
def checkstyleSuppressionConfigDir = file("${rootDir}/suppressCheckstyle")
// ================ [Append] ================
dependencies {
checkstyle 'package.to.my.check.module:mycheck-module:[version]'
}
// ================ [Append] ================
checkstyle {
toolVersion = '8.37'
sourceSets = [it.sourceSets.main]
config = resources.text.fromString(getClass().getResourceAsStream('checkstyle.xml').text)
ignoreFailures = false
maxWarnings = 0
maxErrors = 0
configProperties.checkstyleSuppressionConfigDir = checkstyleSuppressionConfigDir
}

Related

Caused by: java.util.zip.ZipException: invalid code lengths set

I get this error while trying to build a project on Android Studio. It happens when trying to compress the final artifact with a binary file on res/raw/file.dat.
The solution like explained here: Maven corrupting binary files in source/main/resources when building jar
Is to add the file or res/raw folder to a "false" filtering on build. But the problem is that i configure my internal maven repository with JFrog Articatory following this: https://inthecheesefactory.com/blog/how-to-setup-private-maven-repository/en
The question:
How to convert this:
<project>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources/lib</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
...
</plugins>
</build>
</project>
Into gradle style:
def libraryGroupId = 'XXXXXXX'
def libraryArtifactId = 'XXXXXX'
def libraryVersion = '0.0.1'
publishing {
publications {
aar(MavenPublication) {
groupId libraryGroupId
version libraryVersion
artifactId libraryArtifactId
artifact("$buildDir/outputs/aar/${artifactId}-release.aar")
}
}
}
artifactory {
contextUrl = 'XXXXXXXXXX'
publish {
repository {
repoKey = 'libs-release-local'
username = artifactory_username
password = artifactory_password
}
defaults {
publications('aar')
publishArtifacts = true
properties = ['qa.level': 'basic', 'q.os': 'android', 'dev.team': 'core']
publishPom = true
}
}
I've tried using pom.xml but it kepts saying does not recognized TAG project in .xml file.
Thanks!!!
android {
sourceSets {
main {
assets.srcDirs = ['src/main/res/raw']
}
}
}

Maven: How to use a build.gradle file?

I'm a complete beginner with both Maven and Gradle. I have a working Maven project. Now I want to add a bunch of classes from another project which was built using Gradle. I want to take classes from the Gradle project and use it in my Maven project. When I copy the classes into my Maven project, I get a bunch of dependency errors. To resolve those errors I would like to add the required stuff to my pom.xml file.
Basically, I want to add all dependencies specified in a build.gradle file, to my pom.xml file.
The pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myGroupId</groupId>
<artifactId>myArtifactId</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>10</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jgrapht/jgrapht-core -->
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jgrapht/jgrapht-jdk1.5 -->
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-jdk1.5</artifactId>
<version>0.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jgrapht/jgrapht-ext -->
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-ext</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0-jre</version>
<!-- or, for Android: -->
<!-- <version>27.0-android</version> -->
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.trove4j/trove4j -->
<dependency>
<groupId>net.sf.trove4j</groupId>
<artifactId>trove4j</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
</project>
The build.gradle file:
buildscript {
repositories {
jcenter()
maven { url 'https://maven.rapidminer.com/content/groups/public/' }
}
}
plugins { id 'com.rapidminer.extension' version '0.8.2' }
// Define Maven artifact repositories
repositories {
jcenter()
maven { url 'https://maven.rapidminer.com/content/groups/public/' }
ivy {
url "https://github.com/rapidprom/rapidprom-libraries/raw/${project.properties["version"]}/prom/"
layout "pattern", {
artifact "[module]-[revision]/[module]-[revision].[ext]"
artifact "[module]-[revision]/[artifact].[ext]"
ivy "[module]-[revision]/ivy-[module]-[revision].xml"
}
}
ivy {
url "https://github.com/rapidprom/rapidprom-libraries/raw/${project.properties["version"]}/thirdparty/lib/"
layout "pattern", {
artifact "[module]-[revision]/[module]-[revision].[ext]"
ivy "[module]-[revision]/ivy-[module]-[revision].xml"
}
}
ivy {
url "https://github.com/rapidprom/rapidprom-libraries/raw/${project.properties["version"]}/thirdparty/resource/"
layout "pattern", {
artifact "[module]-[revision]/[module]-[revision].[ext]"
ivy "[module]-[revision]/ivy-[module]-[revision].xml"
}
}
}
extensionConfig {
// The extension name
name 'RapidProM'
/*
* The artifact group which will be used when publishing the extensions Jar
* and for package customization when initializing the project repository.
*
*/
groupId = 'org.rapidprom'
/*
* The extension vendor which will be displayed in the extensions about box
* and for customizing the license headers when initializing the project repository.
*
*/
vendor = "Eindhoven University of Technology"
/*
* The vendor homepage which will be displayed in the extensions about box
* and for customizing the license headers when initializing the project repository.
*
*/
homepage = "www.rapidprom.org"
// enable shadowJar before rapidminer dependency (otherwise build fails)
shadowJar {
zip64 true
}
// define RapidMiner version and extension dependencies
dependencies {
rapidminer '7.2.0'
//extension namespace: 'text', version: '7.2.0'
}
}
// Define third party library dependencies
dependencies {
compile group:"org.rapidprom", name:"ProM-Framework", version:"31245"
compile "org.rapidprom:AcceptingPetriNet:6.7.93"
compile "org.rapidprom:AlphaMiner:6.5.47"
compile "org.rapidprom:Animation:6.5.50"
compile "org.rapidprom:ApacheUtils:6.7.88"
compile "org.rapidprom:BasicUtils:6.7.104"
compile "org.rapidprom:BPMN:6.5.56"
compile "org.rapidprom:BPMNConversions:6.5.48"
compile "org.rapidprom:CPNet:6.5.84"
compile "org.rapidprom:DataAwareReplayer:6.7.563"
compile "org.rapidprom:DataPetriNets:6.5.291"
compile "org.rapidprom:DottedChart:6.5.17"
compile "org.rapidprom:EvolutionaryTreeMiner:6.7.154"
compile "org.rapidprom:EventStream:6.5.72"
compile "org.rapidprom:FeaturePrediction:6.5.61"
compile "org.rapidprom:Fuzzy:6.5.33"
compile "org.rapidprom:GraphViz:6.7.185"
compile "org.rapidprom:GuideTreeMiner:6.5.18"
compile "org.rapidprom:HeuristicsMiner:6.5.49"
compile "org.rapidprom:HybridILPMiner:6.7.116"
compile "org.rapidprom:InductiveMiner:6.7.297"
compile "org.rapidprom:InductiveVisualMiner:6.7.413"
compile "org.rapidprom:Log:6.7.293"
compile "org.rapidprom:LogDialog:6.5.42"
compile "org.rapidprom:LogProjection:6.5.38"
compile "org.rapidprom:ModelRepair:6.5.12"
compile "org.rapidprom:Murata:6.5.54"
compile "org.rapidprom:PetriNets:6.7.95"
compile "org.rapidprom:PNetAlignmentAnalysis:6.5.35"
compile "org.rapidprom:PNAnalysis:6.7.122"
compile "org.rapidprom:PNetReplayer:6.7.99"
compile "org.rapidprom:PTConversions:6.5.3"
compile "org.rapidprom:PomPomView:6.5.36"
compile "org.rapidprom:SocialNetwork:6.5.35"
compile "org.rapidprom:StreamAlphaMiner:6.5.15"
compile "org.rapidprom:StreamAnalysis:6.5.38"
compile "org.rapidprom:StreamInductiveMiner:6.5.42"
compile "org.rapidprom:TransitionSystems:6.7.71"
compile "org.rapidprom:TSPetriNet:6.5.35"
compile "org.rapidprom:Uma:6.5.46"
compile "org.rapidprom:Woflan:6.7.59"
compile "org.rapidprom:XESLite:6.7.217"
compile "org.rapidprom:Weka:6.7.3"
}
To deploy to Maven, use the maven plugin.
apply plugin: 'maven'
group = 'com.company'
version = '1.0.0.6'
// To build and push development snapshots, add a suffix to the name
// version = '1.0.0.6-SNAPSHOT'
artifacts {
archives jar
}
uploadArchives {
repositories {
mavenDeployer {
snapshotRepository(url: 'https://maven.repo/bla') {
authentication(userName: snapshotUser, password: snapshotPassword)
}
repository(url: 'https://maven.repo/bla') {
authentication(userName: releaseUser, password: releasePassword);
}
}
}
}
You can both install to a remote repository by running the uploadArchives task, (example above is https://maven.repo/bla), or install to ~/.m2/repository which is maven local using the install task.
gradle install uploadArchives
If you choose to install to maven local, you can add a dependency on maven local to other gradle projects by using dependencies { mavenLocal() }, or including it in your pom.xml
<settings <!-- bla --> >
<localRepository>${user.home}/.m2/repository</localRepository>
</settings>

no main manifest attribute, in application.jar

I am getting the following error while spring boot jar in CI environment
java -jar application.jar
no main manifest attribute, in application.jar
The weird thing is, it does not give the issue in my local or jenkins slave. It starts all right.
It faces issue when I upload the jar to Nexus artifactory and download it on my CI environment.
Building jar using
gradle clean build -x test
My gradle.build file
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
jcenter()
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'org.akhikhl.gretty:gretty:1.2.4'
classpath 'org.ajoberstar:gradle-jacoco:0.1.0'
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:1.2'
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE")
}
}
repositories {
mavenCentral()
mavenLocal()
maven {
credentials {
username "hello"
password "world"
}
url "Nexus URL"
}
}
apply plugin: 'maven-publish'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'spring-boot'
apply plugin: "org.sonarqube"
apply plugin: 'jacoco'
group = 'com.company.pod'
/* Determining version from jenkins pipeline, otherwise is set to 1.0.0-SNAPSHOT */
version = new ProjectVersion(1, 0, System.env.SOURCE_BUILD_NUMBER, System.env.RELEASE_TYPE)
println(version)
class ProjectVersion {
Integer major
Integer minor
String build
String releaseType
ProjectVersion(Integer major, Integer minor, String build, String releaseType) {
this.major = major
this.minor = minor
this.build = build
this.releaseType = releaseType
}
#Override
String toString() {
String fullVersion = "$major.$minor"
if(build) {
fullVersion += ".$build"
}
else{
fullVersion += ".0"
}
if(releaseType) {
fullVersion += "-RELEASE"
}
else{
fullVersion += "-SNAPSHOT"
}
fullVersion
}
}
/*Sonarqube linting of your repository.*/
sonarqube {
properties {
property "sonar.language", "java"
}
}
/* Please don't comment out the part below
To run the same on your laptops/prod boxes/CUAT boxes, just edit the gradle.properties file.
(It will be present in the home directory of the user you are using to run gradle with.`sudo` means root user and likewise)
Enter the following lines(and yes, it will run without values, thank you gradle!)
nexusUrl=
nexusRelease=
nexusSnapshot=
nexusUsername=
nexusPassword=
*/
uploadArchives {
repositories {
mavenDeployer {
repository(url: nexusUrl+"/"+nexusRelease+"/") {
authentication(userName: nexusUsername, password: nexusPassword)
}
snapshotRepository(url: nexusUrl+"/"+nexusSnapshot+"/"){
authentication(userName: nexusUsername, password: nexusPassword)
uniqueVersion = false
}
}
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
publishing {
publications {
maven(MavenPublication) {
groupId 'com.company.something'/*This is different from group variable before*/
artifactId 'something'
version '2.0.0'
from components.java
}
}
}
/*
publishing {
repositories {
maven {
url "~/.m2/repository/"
}
}
}
*/
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
distributionUrl = "https://services.gradle.org/distributions/gradle-$GradleVersion-all.zip"
}
dependencies {
compile('org.projectlombok:lombok:1.16.6')
compile("com.company.commons:company-app:0.0.1-RELEASE")
compile group: 'com.google.guava', name: 'guava', version: '19.0'
compile("com.squareup.retrofit2:retrofit:2.0.1")
compile("com.squareup.retrofit2:converter-jackson:2.0.1")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7'
compile("com.getsentry.raven:raven-logback:7.2.2")
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("com.h2database:h2")
testCompile("junit:junit")
// testCompile("org.springframework.boot:spring-boot-starter-test")
// testCompile group: 'org.hibernate', name: 'hibernate-validator', version: '4.2.0.Final'
}
Articles consulted with no vain
1. Gradle- no main manifest attribute
2. http://www.vogella.com/tutorials/Gradle/article.html#specifying-the-java-version-in-your-build-file
3. Can't execute jar- file: "no main manifest attribute"
The default packaged jar file does not contain MANIFEST file.
I don't know how to configure in Gradle, but in Maven when I added plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>vn.dung.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
And it works for me.
You could change that Maven plugin config to Gradle.
i had to run gradle bootRepackage after building the jar and git a jar that I could execute!
It worked with
gradle upload -x jar
In build.gradle, I noticed boot repackaging was disabled so I enabled it :
bootRepackage {
enabled = true
}
Hi I had exactly same problem, and I found the answer in:
Spring Boot Upload BootRepackage Executable Jar
I used the solution 2
publish {
dependsOn assemble
}

Using postgresql driver with Maven

I tried to use following dependency in Maven:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1200-jdbc4</version>
</dependency>
When I used this driver in the main method, it works, but when I installed the module and tried to run the .jar file, a ClassNotFoundException occurs. What should I do?
I call the driver in the runtime by Class.forName("org.postgresql.Driver"). I tried to change the scope in dependency to runtime, but it didn't help.
My stacktrace:
java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at dbchecker.CountDBChecker.openConnection(CountDBChecker.java:149)
at dbchecker.CountDBChecker.main(CountDBChecker.java:165)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
I used the jar by command java -jar and also in iPOJO (like in this tutorial).
I resolved problem with jar executing by maven-assembly-plugin, but I have another connected problem with iPOJO. The jar is not executed by iPOJO and it seems like some instructions in manifest file are missing. Previous manifest:
Manifest-Version: 1.0
Export-Package: dbchecker;uses:="common,checker.service";version="1.0.
0"
iPOJO-Components: instance { $component="dbchecker.CountDBChecker" }co
mponent { $name="dbchecker.CountDBChecker" $classname="dbchecker.Coun
tDBChecker" provides { }manipulation { $classname="dbchecker.CountDBC
hecker" interface { $name="checker.service.CheckerService" }field { $
name="connection" $type="java.sql.Connection" }field { $name="dateCol
umn" $type="java.lang.String" }field { $name="dateFrom" $type="java.l
ang.String" }field { $name="dateTo" $type="java.lang.String" }field {
$name="interval" $type="long" }field { $name="maxRequired" $type="lo
ng" }field { $name="minRequired" $type="long" }field { $name="report"
$type="common.Report" }field { $name="send" $type="boolean" }field {
$name="table" $type="java.lang.String" }field { $name="zdegeokodowan
o" $type="common.Zdegeokodowano" }field { $name="zdegeokodowanoColumn
" $type="java.lang.String" }method { $name="$init" }method { $name="g
etInterval" $return="long" }method { $name="check" $return="common.Re
port" }method { $name="buildCountQuery" $return="java.lang.String" }m
ethod { $name="executeCountQuery" $return="long" $arguments="{java.la
ng.String}" $names="{query}" }method { $name="setMessage" $return="ja
va.lang.String" $arguments="{long,java.lang.String}" $names="{result}
" }method { $name="setErrorMessage" $return="java.lang.String" $argum
ents="{long,java.lang.String,long,boolean}" $names="{result,query,req
uired}" }method { $name="reportErrorMessage" $arguments="{long,java.l
ang.String,long,boolean}" $names="{result,query,required}" }method {
$name="openConnection" $return="java.sql.Connection" }}}
Bundle-Version: 1.0.0
Built-By: mk
Build-Jdk: 1.7.0_79
Tool: Bnd-1.50.0
Bundle-Name: countDBChecker
Bnd-LastModified: 1442913271587
Created-By: Apache Maven Bundle Plugin & iPOJO 1.12.1
Bundle-ManifestVersion: 2
Import-Package: org.osgi.service.log;version=1.3, common;version="[1.0
,2)", org.apache.felix.ipojo.architecture;version="[1.12.1,2.0.0)", c
hecker.service;version="[1.0,2)", org.apache.felix.ipojo;version="[1.
12.1,2.0.0)", org.osgi.service.cm;version=1.2
Bundle-SymbolicName: supervisor.countDBChecker
Recent manifest:
Manifest-Version: 1.0
Created-By: 24.79-b02 (Oracle Corporation)
Archiver-Version: Plexus Archiver
Should I change the execution goal in my pom.xml? This is the pom (part):
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-ipojo-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>ipojo-bundle</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Looks like your JAR doesn't contain PostgreSQL JDBC driver classes. Just specify classpath to JAR when running your app, e.g.:
java -cp postgres-jdbc.jar:myApp.jar mypackage.MyMainClass
Or use maven-assembly-plugin to build standalone JAR including PostgreSQL dependencies.
As you running .jar file - dependency libraries should be bringed by yourself (they wouldn't be packed into your jar-file).
So you should provide jar for this dependency in classpath for your running .jar file, ie you should:
provide correct manifest file with this dependency's jar as part of classpath or provide classpath for dependecy's jar as part of command line
copy this dependency's jar to target folder

Generates the OSGI-INF/serviceComponent.xml using gradle

OSGI-INF/serviceComponent.xml can be generated using maven scr felix plugin by adding dependency like
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.15.0</version>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
but for gradle I am not able to generate.. I have tried to add the
buildscript {
dependencies {
classpath group:'be.jlr-home.gradle' , name:'scrPlugin', version:'0.1.0'
}}
apply plugin: 'java'
`apply plugin: 'eclipse'
apply plugin: 'maven'
apply plugin: 'osgi'
apply plugin: 'scr'
It's giving error that be.jlr-home.gradle not found.
I am doing something wrong???
basically I need the dependency to add in gradle to generate the servicecomponent.xml
Your maven pom snippet configures the maven plugin for scr from felix. You need a gradle plugin for scr annotation processing. I don't know that felix has one. The bnd project has added gradle support (2.4.0.M1 includes a gradle plugin for bnd) and bnd can process annotations for DS (but maybe not the one's from Felix).
I have resolved the issue
add the below lines to your gradle file and it will work.
ant.properties.src = 'src/main/java'
ant.properties.classes = 'build/classes/main'
task genscr(dependsOn: compileJava) << {
println ant.properties.classes
ant.taskdef(resource: 'scrtask.properties', classpath: configurations.compile.asPath)
ant.scr(srcdir: ant.properties.src, destdir: ant.properties.classes, classpath: configurations.compile.asPath)
}
jar.dependsOn(genscr)
jar {
manifest {
// version = '1.0'
name = 'xxxxxxxxx'
instruction 'Service-Component', 'OSGI-INF/<PKGNAME>.<CLASSNAME>.xml'
}
dependencies {
......//All other dependencies........
compile 'org.apache.felix:org.apache.felix.scr.annotations:1.9.8'
compile 'org.apache.felix:org.apache.felix.scr.ds-annotations:1.2.4'
compile group: 'org.apache.felix', name: 'org.apache.felix.scr.ant', version: '1.110.'
}
sourceSets.main.compileClasspath += configurations.compile
#saviour23's solution did not work for me
It turns out we need to change the buildscript, and that gradle 1.6 does not work; after I upgraded to gradle 2.1, it works fine.
java code:
import org.apache.felix.scr.annotations.*;
#Component
public class Bndtest {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'be.jlr-home.gradle:scrPlugin:0.1.3'
}
}
apply plugin: 'java'
apply plugin: 'osgi'
apply plugin: 'scr'
apply plugin: 'groovy'
apply plugin: 'maven'
sourceCompatibility = 1.6
targetCompatibility = 1.6
version = '0.0.1-SNAPSHOT'
group = 'be.jlrhome.gradle.scr'
description = 'Gradle scr example'
dependencies {
compile 'org.apache.felix:org.apache.felix.scr.annotations:1.9.8'
}
gradle will wrap a jar with correct MANIFEST and OSGI XML

Categories