I'm working on creating a log-in screen to be used with multiple different android applications. What would be the best way to package it so that other people could use my log-in function on their apps. It would be preferred that it would auto-sync for them in-case we were to make changes.
***EDIT****
It seems packaging it into a library module is the best option. How does one go about uploading this module so that if we make an update to this module it will seamlessly update without having to pull from github for example.
Thanks!
If you've pushed your code to GitHub then sharing the library (aar) is easy with JitPack.
Your users will just need to add the repository to their build.gradle:
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
and then your GitHub repository as dependency:
dependencies {
// ...
compile 'com.github.YourUsername:Repo:Release'
}
The nice thing is that you don't have to upload your library. Behind the scenes JitPack will check out the code from GitHub and compile it. As you publish a new release on GitHub it becomes available for others to use.
There is also a guide on how to prepare an Android project.
Make the relevant classes into a library module - you already seem to know how to do that - and then use the Gradle Bintray plugin to upload it to JCenter.
Let's say you set group in build.gradle to com.ryan-newsom, version to 1.0 and the project name is android-log-in-screen.
(part of) android-log-in-screen/build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:0.6"
}
}
apply plugin: 'com.jfrog.bintray'
group = 'com.ryan-newsom'
version = '1.0'
bintray {
// Omitted for brevity, refer to the examples on GitHub.
}
You (or anyone else) can then use it in your project by adding the following:
(part of) other-project/build.gradle:
repositories {
jcenter()
}
dependencies {
compile "com.ryan-newsom:android-log-in-screen:1.0"
}
The library will then be pulled from JCenter and added to the classpath.
You can package the library into an AAR format. It will also contain the resources you used in your login module. After that you can push the AAR library format to bintray (which is free, and allows you to setup your own repository).
Your collaborators can then access the library using a dependency that looks like:
compile 'com.newsom:awesome-login-screen:0.5'
Check this starter tutorial if you are using AndroidStudio/Gradle and would like to push it to bintray. https://github.com/jimcoven/android-bintray-kit
The best way to create a lib and make it available to other developers is creating a AAR so that developers can import it in their project using
dependencies.
The process is quite long.
These are the main steps you should follow to publish your lib:
Register an account and create a new ticket
(https://issues.sonatype.org)
Download (if you use OS X) GPGTools
(http://www.gpgtools.org/)
Modify project gradle files
Create signing key Build
sign and publish your files to the Staging repository
I wrote a post about it and to have more details you can look here.
This is a piece of gradle file called maven_push.gradle:
apply plugin: 'maven'
apply plugin: 'signing'
def sonatypeRepositoryUrl
if (isReleaseBuild()) {
println 'RELEASE BUILD
sonatypeRepositoryUrl = hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
: "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
} else {
println 'SNAPSHOT BUILD'
sonatypeRepositoryUrl = hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
: "https://oss.sonatype.org/content/repositories/snapshots/"
}
def getRepositoryUsername() {
return hasProperty('nexusUsername') ? nexusUsername : ""
}
def getRepositoryPassword() {
return hasProperty('nexusPassword') ? nexusPassword : ""
}
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
pom.artifactId = POM_ARTIFACT_ID
repository(url: sonatypeRepositoryUrl) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
pom.project {
name POM_NAME
packaging POM_PACKAGING
description POM_DESCRIPTION
url POM_URL
scm {
url POM_SCM_URL
connection POM_SCM_CONNECTION
developerConnection POM_SCM_DEV_CONNECTION
}
licenses {
license {
name POM_LICENCE_NAME
url POM_LICENCE_URL
distribution POM_LICENCE_DIST
}
}
developers {
developer {
id POM_DEVELOPER_ID
name POM_DEVELOPER_NAME
}
}
}
}
}
}
signing {
required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
sign configurations.archives
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.allJava
classpath += project.files(android.plugin.getRuntimeJarList().join(File.pathSeparator))
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
//basename = artifact_id
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
//basename = artifact_id
from android.sourceSets.main.allSource
}
artifacts {
//archives packageReleaseJar
archives androidSourcesJar
archives androidJavadocsJar
}
}
while gradle.properties is:
VERSION_NAME=
VERSION_CODE=1
GROUP=
POM_DESCRIPTION=
POM_URL=
POM_SCM_URL= POM_SCM_CONNECTION=
POM_SCM_DEV_CONNECTION=scm:git#github.com:
POM_LICENCE_NAME=The Apache Software License, Version 2.0 POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt POM_LICENCE_DIST=repo
POM_DEVELOPER_ID=
POM_DEVELOPER_NAME=
There is another way but i did not try it and it seems to be easier.
Give a look at jitpack.
Hope it helps you.
make the package or jar depending on your source, and post it on git hub the you can refer to the git from your ide to import or check for updates.
Related
The big picture
I have a library which is used as a dependency in other projects. The library has some configuration requirements related to test suites for each project depending on it.
This is why I have created a plugin that configures those for me so that I could just add the plugin and be done with it:
plugins {
id("org.my.gradle.plugin") version "internal"
}
Project structure
+ root
+ my-api/... (self-sustained; doesn't depend on anything, pretty much interfaces)
+ my-implementation/... (depends on my-api and `my-gradle-plugin` via `plugins { id("org.my.plugin") }`)
+ my-gradle-plugin/... (the plugin itself)
+ build.gradle.kts
plugins {
`java-gradle-plugin`
`maven-publish`
}
gradlePlugin {
plugins {
create("org.my.gradle.plugin") {
id = "org.my.gradle.plugin"
group = "org.my.gradle.plugin"
implementationClass = "org.my.gradle.plugin.MyGradlePlugin"
version = project.version
}
}
}
+ build.gradle.kts // a bunch of shared task configurations and some repository configuration (mavenLocal, maven(xyz), mavenCentral())
+ gradle.properties - contains only "version=0.0.3-SNAPSHOT"
+ settings.gradle.kts
pluginManagement {
repositories {
mavenLocal()
maven { url = uri("https://xyz") }
gradlePluginPortal()
mavenCentral()
}
resolutionStrategy {
val version: String by settings
eachPlugin {
if (requested.id.id == "org.my.gradle.plugin") {
useVersion(version)
}
}
}
}
The problem
The plugin works fine if it's a separate stand alone project. This, unfortunately, complicates my workflow so I thought I could possibly have it within the same multi-module project.
Unfortunately I am running into problems, because gradle doesn't seem to know it needs to build the plugin in order to use it inside one of the modules and therefore I get:
* Where:
Build file '/workspace/my-implementation/build.gradle.kts' line: 3
* What went wrong:
Plugin [id: 'org.my.gradle.plugin', version: '0.0.3-SNAPSHOT'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'org.my.gradle.plugin:org.my.gradle.plugin.gradle.plugin:0.0.3-SNAPSHOT')
Searched in the following repositories:
MavenLocal(file:/home/jenkins/.m2/repository/)
maven(https://xyz)
Gradle Central Plugin Repository
MavenRepo
Why does it fail?
I know that spring-boot-gralde-plugin is part of the spring boot multi-module gradle project and is consumed within the project itself. Unfortunately I haven't been able to figure out what's the "magic trick" to make it work.
Any ideas what I'm missing here?
Well, this took me the better half of the day, but I finally figured it out.
In order to consume the plugin inside the same repository, you must place a settings.gradle.kts file inside your plugin. This separates the plugin build from the rest of your modules. Then you can use includeBuild() inside the pluginManagement to enforce the building of the plugin before everything else:
// ./root/settings.kotlin.kts
rootProject.name = "root-project"
include("my-api", "my-implementation")
pluginManagement {
includeBuild("my-gradle-plugin")
repositories {
mavenLocal()
maven { url = uri("https://xyz") }
gradlePluginPortal()
mavenCentral()
}
resolutionStrategy {
val version: String by settings
eachPlugin {
if (requested.id.id == "org.my.gradle.plugin") {
useVersion(version)
}
}
}
}
Then you can just use the plugin without any issues:
plugins {
id("org.my.gradle.plugin") version "internal"
}
I'm using gradle 7.5+ so not sure if this works for older versions.
The docs also mention this here:
https://docs.gradle.org/7.6/userguide/composite_builds.html#included_plugin_builds
I am trying to convert a maven project to gradle. Our maven project uses a custom repository for dependencies, and we can not download anything from ordinary repos. When running command
gradle init -I ./init.gradle
I am getting errors and I can see on the command line that it is trying to connect to the https://repo.maven.apache.org/maven2. I have tried using the init.gradle script from userhome and gradlehome, but there was no change. I also added the custom repo to pom.xml since I saw someone say gradle uses those if available, but it didn't. This is my init.gradle for reference:
apply plugin: EnterpriseRepositoryPlugin
class EnterpriseRepositoryPlugin implements Plugin<Gradle> {
void apply(Gradle gradle) {
gradle.settingsEvaluated { settings ->
settings.pluginManagement {
repositories {
maven {
url "custom_repository_url"
}
}
}
}
}
}
I also tried this at first:
allprojects {
buildscript {
repositories {
mavenLocal()
maven { url "custom_repository_url"}
}
}
repositories {
mavenLocal()
maven {
url "custom_repository_url"
}
}
}
I have also tried adding a build.gradle with the following task and running it:
tasks.register('showRepos') {
doLast {
println "All repos:"
println repositories.collect { it.name }
}
}
This only printed out the custom repo. However deleting this and running gradle init still connects to the https://repo.maven.apache.org/maven2.
Edit:
It seems this question won't be solved. In case you found this when looking for an answer, I have found this solution that might work for you. I wasn't so lucky.
https://github.com/gradle/gradle/issues/19884
I have also crated an issue on github. Maybe there will be an answer there.
https://github.com/gradle/gradle-native/issues/1097
Is there a way to change the location of the default repositories in a projects build.gradle? I'm using a company PC which has a ~/.gradle/init.gradle file that globally specifies the default gradle repos like:
allProjects {
buildscript {
repositories {
mavenLocal()
maven { url "<company url>"}
}
}
repositories {
mavenLocal()
maven { url "< company repo url>"}
}
}
I can only access this while on the company network which means for any side project the build fails if I don't log on. Is there a way I can override this repo in the project to point to a different (public) repo? I've tried various things like changing the project build.gradle to have:
repositories {
maven {
url "https://maven.springframwork.org/release"
}
}
But it still tries to point to the company repo and fails to resolve it.
If you need to add some repositories for dependencies - you can do it right in the build.gradle.
If you want to override Gradle plugin repositories, defined in global init script, only for a specific project(s), you need to:
Create an init script, which would be executed after init.gradle in USER_HOME/.gradle/ directory (for instance, init.gradle.kts in the USER_HOME/.gradle/init.d/ directory)
Use some if-condition to specify what projects will be covered with this override (for instance, in some specific folder). Gradle scripts are not only a DSL, you can use all power of Turing-complete Groovy/Kotlin.
I'll use Kotlin here (probably, it's a valid Groovy code too):
settingsEvaluated {
if (rootProject.projectDir.toString().startsWith("path/to/my/side/projects/folder")) {
pluginManagement {
repositories {
gradlePluginPortal()
mavenLocal()
}
}
}
}
I might be missing something major here. However, I am struggling to publish a simple library to a maven repository (which will be consumed by other maven based projects in the organization)
The best guide I've found is on the official Gradle website: https://docs.gradle.org/current/userguide/publishing_maven.html
However, there are still many unanswered questions:
Is there no way to differentiate between SNAPSHOT and release builds other than to manually include the if-else statement?
What is from components.java? IDEA gives no autocomplete or documentation on most of these DSLs (unlike Maven, where the code intelligence works well)
How do I publish to a private repository that requires authentication? I understand somewhere there must be a block that uses:
username = "${artifactory_user}"
password = "${artifactory_password}"
With the values being read from ~/.gradle/gradle.properties
But where do I put this block?
Overall, I feel like I a missing something here, maybe some documentation that is popularly read ... using maven itself the process is fairly straight-forward and the official documentation makes the process relatively painless
With Gradle, I feel like the simplest publish to a repository requires quite a lot what feels like customized logic when my intuition says something so common must already be encapsulated in a plugin with reasonable defaults
I see you've found your solution already, but my answer will aim to give you detailed answers to your questions.
Is there no way to differentiate between SNAPSHOT and release builds other than to manually include the if-else statement?
Correct. An if-else statement is exactly what you need to distinguish between a snapshot and release build. Gradle itself does not provide any sort of versioning functionality. That is left to you to handle or a plugin such as Nebula Release.
What is from components.java
The from is a method call from AbstractCopyTask which the Jar task type is a subclass of.
components is again another method call. You are actually calling getComponents() of Project.
components.java is sugar for components.getByName("java"). This works because of dynamic/magic of Groovy.
IDEA gives no autocomplete or documentation on most of these DSLs (unlike Maven, where the code intelligence works well)
This is due to the dynamic/weak typing of Groovy. The build.gradle file is written using Groovy. IntelliJ does try to infer the type of your build script, but it can't fully. Luckily you can now write your build script using Kotlin:
https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/
https://docs.gradle.org/current/userguide/kotlin_dsl.html
I highly suggest using the Kotlin DSL going forward. You will know exactly where everything is coming from.
How do I publish to a private repository that requires authentication?
Unfortunately the docs for the maven-publish plugin merely mentions it in a single sentence. Even so, it just directs you to API docs which aren't always helpful, but you were able to figure it out.
https://docs.gradle.org/current/userguide/publishing_maven.html
You can also configure any authentication details that are required to connect to the repository. See MavenArtifactRepository for more details.
And finally:
(...) the values being read from ~/.gradle/gradle.properties
Gradle will go out of its way to resolve a property. gradle.properties is just one of many locations that Gradle will look for properties. You can see more details under the Properties section on top here.
I'd like to finish off by providing a complete example of your answer using the Kotlin DSL. Also using the buildscript { } is a legacy method of applying plugins as noted here. You should use the newer/preferred plugins { } block going forward. More info here.
plugins {
`maven-publish`
id("org.jetbrains.kotlin.jvm") version "1.3.31"
}
group = "com.company"
version = "1.0.0-SNAPSHOT"
tasks.wrapper {
gradleVersion = "5.6.1"
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}
val sourcesJar by tasks.registering(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets.main.get().allSource)
}
repositories {
mavenCentral()
}
publishing {
publications {
register<MavenPublication>("mavenJava") {
artifactId = "some-artifactId"
from(components["java"])
artifact(sourcesJar.get())
pom {
name.set("Project Name")
}
}
}
repositories {
maven {
url = uri("https://company.jfrog.io/company/maven-local")
credentials {
username = property("artifactory_user") as String
password = property("artifactory_password") as String
}
}
}
}
val test by tasks.getting(Test::class) {
useJUnitPlatform()
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// ...
}
To answer my own question here is the bare bones minimum to publish to a private repo:
buildscript {
ext.kotlin_version = '1.3.41'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id 'java-library'
id 'maven-publish'
}
apply plugin: 'kotlin'
group 'com.company'
version '1.0.0-SNAPSHOT'
wrapper {
gradleVersion = '4.9'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
repositories {
mavenCentral()
}
publishing {
publications {
mavenJava(MavenPublication) {
artifactId = 'some-artifactId'
from components.java
artifact sourcesJar
pom {
name = 'Project Name'
}
}
}
repositories {
maven {
url = "https://company.jfrog.io/company/maven-local"
credentials {
username = "${artifactory_user}"
password = "${artifactory_password}"
}
}
}
}
test {
useJUnitPlatform()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
compile "com.squareup.moshi:moshi-kotlin:1.8.0"
compile "com.squareup.moshi:moshi-adapters:1.8.0"
compile "com.squareup.okhttp3:okhttp:4.0.1"
testImplementation "io.kotlintest:kotlintest-runner-junit5:3.1.0"
}
I'm looking for gradle to create a clean pom with just the bare essentials like dependencies so I can upload it along the the jar, sources.jar, and javadoc.jar.
I also don't want to have to manually create the pom.
Have a look at publishing, in particular with the maven-publish plugin, which handles this for you indeed.
But in order to have the minimal publication, this is a simple as:
plugins {
`java`
`maven-publish`
}
group = "org.example"
version = "1.0"
// dependencies declaration omitted
publishing {
publications {
create<MavenPublication>("myLibrary") {
from(components["java"])
}
}
repositories {
maven {
name = "myRepo"
url = uri("file://${buildDir}/repo")
}
}
}
Note: This uses the Kotlin DSL, the Groovy version has a couple differences, see documentation
And then running ./gradlew publish will publish org.example:<project-name>:1.0