creating a jar and adding to another project as a library - java

I have java project build and executed successfully in IntelliJ using a gradle build tool. I want to create a jar file of this project and add it to the another project so that it can use this as a library. How can I create a jar and add it to another project?

Maven Publish Plugin
The Maven Publish Plugin gives you a task called publishToMavenLocal. If you run that task like so in the project that is the one you wish to publish:
./gradlew publishToMavenLocal
this will publish a jar that you can pull into another project, assuming you have your group, module and version setup properly.
The group property you can set in your build.gradle, version in the gradle.properties and the module will be the name of the module that is being built unless specified otherwise.
You can check in your ~/.m2 directory to see that they were published with the correct group, module, and version.
Then in your consuming module, you can specify mavenLocal() in your repositories dsl block, and declare your published jar that you want to consume just like any other dependency.

Related

How to build a jar without "-SNAPSHOT" in the name with Gradle?

Every time I run this command: gradle build it produces a my-program-1.0.0-SNAPSHOT.jar. Where do I control this? In the gradle.properties I have only the version nothing else. Even if I run gradle -Dversion=1.0.0 build it still creates a jar with SNAPSHOT in the name. How do I create a jar like this: my-program-1.0.0.jar?
I'm using spring boot.
Basically we have to change the project version in the gradle, To do so we have to use project api in gradle. you can refer https://docs.gradle.org/current/dsl/org.gradle.api.Project.html
One Solution I can suggest is:
Add version '1.0' in your build.gradle

How install in local maven repo a sub-project via gradle (in a function)?

In gradle, when running my script, I have a bunch of sub-projects that are dependent on others. We need to pull them in as external jars (not compile project for various reasons). So I need them installed locally. I want this to be a function defined in the parent gradle.
When I try to call install ":projName" inside a function, I get the error:
Could not find method install() for arguments [:projName] on root project 'test' of type org.gradle.api.Project
How would I do this?
ext.installLocal = {moduleName ->
//...elided... doing some checking, conditionals, etc
//Try to install locally
install ":${moduleName}"
}
You need to add build.finalizedBy(publishToMavenLocal) command to all your subprojects build.gradle file that need to be pushed in your maven local. If your need to push all your subprojects to maven local, you can call this command in subprojects{ } closure in main build.gradle file. But remember if subprojects are dependent on each other their build order is critical.

Use build.gradle for deployment

I'm learning how to use gradle to build my java applications. Currently I'm using eclipse with buildship plugin. I have it building my JARs and WARs, and also have gradle running my JUnit and Selenium tests. I like that it pulls in the dependencies I need as I need them during development.
It seems like it would make sense if my build.gradle files define my dependencies to build and run my application in dev then I should be able to use them for deployment. Otherwise I have to retrieve all my dependencies by some other means and deploy to my production environment, and managing 2 different methods of retrieving and deploying dependencies seems to be a risk for problems.
Can I use gradle or at least my build.gradle files in some way for my deployment?
Take a look at the gradle distribution plugin. This plugin adds tasks to create an "install folder" or an archive file (zip or tar) containing all the dependencies you'll need to execute/deploy your application.
Gradle application plugin also generates shell/bat scripts to invoke your application.

What are the options of joining the external library in Android using Gradle?

For example: there is a project on GitHub https://github.com/chrisbanes/ActionBar-PullToRefresh, it uses Gradle, so you can add to a project using:
compile 'com.github.chrisbanes.actionbarpulltorefresh: library: +'
And there is a project https://github.com/ahorn/android-rss, where Gradle is not used.
What are the ways to connect using Gradle this library to my project?
This need not to store external libraries in my git repository.
If you have a project that is not a simple Java project and is not made for gradle Im afraid you have to download the source convert an eclipse adt project manually/automatically to gradle:
You cann import it as a module through Android Studio (New-> Module -> Import Existing Project) see http://developer.android.com/sdk/installing/migrate.html
You could also just write the build files yourself teaching you a little gradle on the way, just look at the examples and docs how to do it
This project doesn't use gradle but uses maven so dependency to this project can also be handled. You just need to find repository with public access where this project is deployed and add appropriate address in repositories section in build.gradle file. If there's no such repository You can download the project and install it in the local repository - the downside is that no other developer that works with your project can download this dependency until You make Your repo public.
Furthermore the fact that some project doesn't use gradle or maven doesn't mean that dependency to this project can't be handled with gradle. If this project has fixed versioning scheme and is accessible over the net gradle can be configured to use such dependency. Gradle can deal with multiple types of repositories (e.g. flat files).

Library (jar) dependencies between github repositories

I can't figure out an appropriate way to structure git repositories to handle library dependencies between git repositiories.
I have a number of Java projects that rely on another, frequently updated project that's included in them as a .jar library. I now want to migrate them all to github.
Can I set up the projects in github so that whenever I push project A, then all other projects can pull the new version of projectA.jar automagically? They don't rely on any source files, they just need the latest libraray jar. Currently it's done by an ant script that tries to copy the latest jar from the other project at each build.
Changing your project over to Maven may be a big help; .jar files would no longer be stored in version control.
You would need to open project A, run 'mvn install' to produce the library files on your PC, and then you could use project A's libraries from project B.
Another alternative is to run your own Maven artifacts server, such as Artifactory, Nexus, etc... Project B would look at the artifacts server to see if project A's files are available.

Categories

Resources