What's the successor of `org.gradle.util.VersionNumber` - gradle

In gradle 7.3, I found the following source code:
package org.gradle.util;
import com.google.common.base.Objects;
import com.google.common.collect.Ordering;
import javax.annotation.Nullable;
/**
* This class is only here to maintain binary compatibility with existing plugins.
*
* #deprecated Will be removed in Gradle 8.0.
*/
#Deprecated
public class VersionNumber implements Comparable<VersionNumber> {
...
So is this feature dropped or moved? What's the new way of using VersionNumber related functionality?

That class and many others in the util package were not intended to be used in plugins or anything outside of Gradle's codebase.
In #16745, the Gradle team formally deprecated many of the utility classes and moved/copied them into an internal utility package. Some utility classes have replacements as noted in their respective Javadoc, but some such as VersionNumber have no public replacement provided by Gradle. As such, this should be considered a private API and you should not use it anywhere in your project's build or plugins.
As an alternative, you can use a semver library of your choosing as a replacement.

Related

Adding gradle dependencies

I have a simple gradle 7.2 project, with a simple kotlin file, running java 11, on ubuntu 20.04 in vs code
For my project, I need to add some simple dependencies to java.security such that I'll be able to encrypt and hash some things.
So I need to add it as a dependency.
The project is created by running gradle init and picking all the default options.
I then want to be able to do an import like: import java.security.MessageDigest and use the java.security package.
I guess I'll have to add the dependency in the build file, which currently looks like this:
plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
id("org.jetbrains.kotlin.jvm") version "1.5.0"
// Apply the application plugin to add support for building a CLI application in Java.
application
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Align versions of all Kotlin components
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// This dependency is used by the application.
implementation("com.google.guava:guava:30.1.1-jre")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
application {
// Define the main class for the application.
mainClass.set("com.what.isthis.AppKt")
}
I now search google high and low for how a reference to the java.security package can be added in gradle, but find absolutely nothing anywhere.
Following a guide like this it looks like I could just add a in this manner:
implementation 'org.springframework.boot:spring-boot-starter-validation:2.4.0'
If what I wanted was a reference to this validation library. But I can never get to test it, because I can't find any info on how I would target java.security anywhere.
Looking at the docsI tried to just grab the names I could find here, but this did not compile:
implementation 'java.security Package'
So yes, how do I get thjis dependency. And in general how do find the names that I need for getting dependencies in general?
java.security package is part of the Java language itself as you can see from the documentation, for this reason you don't need to include it explicitly it should already be available to you.
Please make sure you have proper Java SDK set up in IDE. Try to configure different distribution/type than you use currently.
Even if you have logic in Kotlin class it should properly resolve an import and compile.
import java.security.MessageDigest
fun main() {
val test = MessageDigest.getInstance("SHA-256")
println("Test Succeeded")
}
You're not finding examples of declaring java.security packages in Gradle because you don't need to declare them; they're included in the JDK so you can import them natively in any class without declaring them in gradle. Try creating this class in any given package within your project and running it. It should succeed.
import java.security.MessageDigest;
public class Test {
public static void main(String[] args) throws Exception {
MessageDigest test = MessageDigest.getInstance("SHA-256");
System.out.println("Test Succeeded");
}
}

build.gradle.kts syntax sugar - explanation

I was searching in the Internet but I have not found answer for my question regarding to build.gradle.kts syntax.
I haven't found any syntax regarding to below application plugin adding:
plugins {
// Apply the Kotlin JVM plugin to add support for Kotlin on the JVM.
id("org.jetbrains.kotlin.jvm") version("1.3.21")
// Apply the application plugin to add support for building a CLI application.
application
//id("kotlin-android")
I mean, what kind of syntax stay behind application?
It only looks like a class member name. Maybe is it a function call? but it has no brackets.
I don't catch this kotlin syntax sugar.
Additional, I have not found plugins (and others blocks) implementation in gradle repository. Someone know where it is located? I am just curious how it works.
If you go to the implementation of application it should bring you to the source:
/**
* The builtin Gradle plugin implemented by [org.gradle.api.plugins.ApplicationPlugin].
*
* Visit the [plugin user guide](https://docs.gradle.org/current/userguide/application_plugin.html) for additional information.
*
* #see org.gradle.api.plugins.ApplicationPlugin
*/
inline val org.gradle.plugin.use.PluginDependenciesSpec.`application`: org.gradle.plugin.use.PluginDependencySpec
get() = id("org.gradle.application")
So application is just an extension function on PluginDependenciesSpec or plugins { }

missing dependencies in latest spring security test

I need to resolve certain methods (status, jsonPath, content) mentioned in code shown below -
mockMvc.perform(MockMvcRequestBuilders.get("/api/token")
.with(getAuthentication(getOauthTestAuthentication()))
.sessionAttr("scopedTarget.oauth2ClientContext", getOauth2ClientContext()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.username").value("cominventor"))
.andExpect(jsonPath("$.token").value("my-fun-token"));
When I searched for related projects containing these methods, many of them are importing the following
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
The problem is that I am unable to find a counterpart in package spring-security-test with version 4.2.2.RELEASE
The closest options that could have worked but haven't are
import static org.springframework.test.web.servlet.ResultMatcher.*;
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.*;
The above don't contain the methods I am looking for. Any idea where should I look for these methods or their newer counterparts.
Everything worked out once I set the spring-security-test version to 4.0.2.RELEASE
I had the same problem, and I solved it importing the artifact
org.springframework.security:spring-security-test:4.2.3.RELEASE
I am using spring-boot-starter-test:1.5.3, and for some reason the folks at Spring decided not to include spring-security-test in its POM.

Force Imports in Apache Karaf

I have bundle that deals mostly with interfaces and it uses a factory from a thrid party jar to get the instances for the interfaces it is using.
For example,
my-bundle.jar has...
com.oth.itf.Intrface itf = Fctry.getInstance('ABC');
has the
imports-package for com.oth.itf
third-party.jar has..
public static com.oth.itf.Intrface getInstance(String abc) {
if (...) {
return new com.oth.impl.ItfInstance();
}
}
has the exports-package for com.oth.itf and com.oth.impl
Everything works fine and bundles also get deployed but the issue is that I am getting
ClassNotFoundException for ItfInstance
on
my-bundle
when the code gets executed.
I tried adding import-package for com.oth.impl but to no avail. Things started to work when I added a
dummy declaration of com.oth.impl.ItfInstance
some where in my-bundle.jar. Looks like, Karaf gets the imports only if we explicitly use them. Is there a better way? Is there a way to force Karaf to import packages even if we don't use them explicitly?
Neil is right, if it's not used in the code it can't be imported. With Karaf you have the possibility to help. With the command bundle:dynamic-import you can add a dynamical import to the bundle on run-time. With this you're able to find the actually needed imports via bundle:headers you'll find the imported packages of this bundle. Take those and add those missing imports to your manifest generation and your're set.
If you doesn't has bundle: option, uses this dev: command:
dev:dynamic-import <BundleID>
This allows Karaf import dependencies on runtime.

MavenProject: Get the available classes for use on my plugin

I'm loading a Maven project as described here. I'm trying to figure out how I can retrieve the source roots so I can figure out the Java classes I have so my Mojo can use them.
I tried a couple of the methods in there, like getResources or getScriptSources without luck. Any idea?
Thanks in advance!
Edit:
I was asked to elaborate a little bit in what I'm attempting to do, so here it is:
The plugin I'm developing will take the sources in the project and create test cases from those. Unless configured, I want to generate tests for all the classes, and for that, I need to somehow figure out where are my sources so I can configure properly.
Hope that helps.
Here's the repository. I planned on publishing it later but I provided source as requested.
Have you read the plugin developers documentation?
That page will link to Plugins Cookbook which links to Mojo Developer Cookbook which has The maven project, or the effective pom. and gives you access to org.apache.maven.project.MavenProject object via
/** #parameter default-value="${project}" */
private org.apache.maven.project.MavenProject mavenProject;
Alternatively via Java 5 annotations
#Component
MavenProject project;
You can call getCompileSourceRoots() to get a list of the directories that will be used for compilation.
You will also need to do more reading about how to setup inclusion/exclusions. You can use other plugins as examples of how to do this, e.g. maven-compiler-plugin
If you want to use annotations, it is very important to make sure your pom is configured as per using annotations and that you use annotations at the class level as well. Mixing javadoc annotations might not work.
I think the simplest solution would be to define a mojo parameter:
/**
* #parameter default-value="${project.build.sourceDirectory}"
* #required
*/
private File sourceDirectory;
or with new annotation based definition:
#Parameter(required = true, defaultValue="${project.build.sourceDirectory}"}
private File sourceDirectory;
which should give your wished result.

Resources