Generates the OSGI-INF/serviceComponent.xml using gradle - java

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

Related

Move all libs version to root gradle file in multi module gradle project

I have gradle project with modules.
-RootProject
-my-server(spring boot 2server)
-my-generator(module)
Now my root project's gradle file look like this:
group = 'com.mayprojekt'
version = '0.0.1-SNAPSHOT'
Gradle settings file look like this:
rootProject.name = 'root-project'
include 'my-generator'
include 'my-server'
my-server's gradle file look like this:
buildscript {
ext {
springBootVersion = '2.0.3.RELEASE'
cxfVersion = '3.2.2'
uuidGeneratorVersion = '3.1.5'
commonLang3Version = '3.7'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'com.mayprojekt'
version '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
mavenLocal()
}
bootJar {
mainClassName = 'com.mayprojekt.Application'
archiveName = 'my-server.jar'
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter'
compile "org.apache.cxf:cxf-spring-boot-starter-jaxws:${cxfVersion}"
compile "com.fasterxml.uuid:java-uuid-generator:${uuidGeneratorVersion}"
compile "org.apache.cxf:cxf-rt-features-logging:${cxfVersion}"
compile "org.apache.commons:commons-lang3:${commonLang3Version}"
compileOnly('org.projectlombok:lombok')
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("oracle:ojdbc6:11.2.0.3")
compile project(':my-generator')
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
my-generator module just module with some classes.
But I want move all libraries versions to root gradle file. I make it in maven project and I need something look like this:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.ws.version>3.0.0.RELEASE</spring.ws.version>
<jvnet.jaxb2.maven2.version>0.13.2</jvnet.jaxb2.maven2.version>
<cxf.version>3.2.4</cxf.version>
<commons.codec.version>1.11</commons.codec.version>
<springfox.version>2.7.0</springfox.version>
<oracle.version>11.2.0.3</oracle.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
But I don know how can I make this with gradle.
Assuming your my-generator project is a gradle project, then you can do this in the root build.gradle:
...
project(':my-server') {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
....
}
project(':my-generator') {
apply plugin: 'java'
dependencies {
compile 'org.springframework.boot:spring-boot-dependencies:2.0.2.RELEASE'
}
}
...
Using the 'project' clause, you can apply configurations only to children module projects from the root project.
using the 'allprojects' clause you can apply certain configurations to all your child module projects:
allprojects {
dependencies { ... }
}
Check this out https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-creating-a-multi-project-build/
Hope this helps

cannot resolve symbol 'SpringRunner' for java Spring junit

I'm trying to write a junit test for my first java project using Spring framework. I have searched online how to include the dependency in order to use it but keep encountering this warning:
Cannot resolve symbol 'SpringRunner'
in intelliJ IDEA. So what is missing?
Here is my dependency file for gradle.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle- plugin:1.5.1.RELEASE")
classpath("org.springframework:spring-test:4.0.3.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
testCompile("org.springframework:spring-test:4.0.3.RELEASE")
compile('com.google.maps:google-maps-services:0.1.18')
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
compile("org.springframework.boot:spring-boot-starter-web")
testCompile('junit:junit:4.12')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile("org.springframework.security:spring-security-test")
}
try adding org.springframework.test.context.junit4.SpringRunner;

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
}

Gradle parent project not using child repositories?

That's how I would interpret it. I have a multi-project build with 2 modules: codec and content. When I try to build the project it says it can't find repositories. But when building individual modules, no errors. Also, evaluationDependsOn(':codec') doesn't seem to help.
Could not resolve all dependencies for configuration ':compileClasspath'.
> Cannot resolve external dependency commons-codec:commons-codec:1.5 because no repositories are defined.
Required by:
:multiproject-unified:unspecified > multiproject-unified:codec:unspecified
parent build.gradle:
apply plugin: 'java'
dependencies {
compile project(':codec')
compile project(':content')
}
settings.gradle
include 'codec',
'content'
:codec build.gradle
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'commons-codec:commons-codec:1.5'
}
:content build.gradle
apply plugin: 'java'
repositories {
mavenCentral()
}
Try declaring your repositories for the entire project in the parent:
allprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
}

Class in main/resources not found

I'm using the Spring Boot gradle plugin to build an executable war. I have a FindResource.java class in src/main/resources to locate files:
FindResource.class.getResource(templateName).toURI()
When I execute gradle build I get an error, that the class FindResource cannot be resolved. Do I need to the the Spring Boot gradle plugin, that it should also use classes from the resources directory. How can I do so?
My build.gradle looks as follows:
buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'abc'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-jersey")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.apache.pdfbox:pdfbox:1.8.10")
compile('org.apache.poi:poi-ooxml:3.12')
compile('org.apache.poi:poi-scratchpad:3.12')
runtime("org.hsqldb:hsqldb")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
As mentioned in the comment class files to load need to be in src/main/java/ and not in src/main/resources. This link may help give you more information on the convention of this structure.

Categories