I'm writing Java code in a Jupyter Notebook via IJava . I want to add external dependencies such as OpenCSV. Using gradle, this would normally be incorporated in the build.gradle file via the line
compile 'com.opencsv:opencsv:4.3.2'
How can I add dependencies at runtime via gradle in the Jupyter notebook?
There is the maven magic for this. Since the dependency is on maven central, it is as easy as adding %maven com.opencsv:opencsv:4.3.2 in a cell.
For example
%maven com.opencsv:opencsv:4.3.2
import com.opencsv.CSVReader;
// ...
The name maven is slightly misleading as Maven (or Gradle) is a build tool rather than strictly a dependency manager, but as the dependencies are often resolved from the Maven Central repository the notion of adding a "maven dependency" was a good enough metaphor.
Related
I am coming from a C# background. I am used to NuGet and Visual Studio project references so the Java ecosystem has confused me quite a bit.
I have a gradle library project. I want to import org.apache.commons.codec.binary.Base64;
However I keep getting cannot resolve errors.
I am using VSCode as my IDE and I would like to include the codec dependancy. How would I achieve this in VSCode/gradle.
I have downloaded the commons-codec-1.14.jar file, but don't know where to put it in the project.
Gradle is a tool that, among other things, manages your dependencies. This means that, you do not need to manually download and add dependencies to your project. Gradle solves this for you.
See the official documenation on how to handle dependencies with Gradle.
You probably have a build.gradle file, in which you need to include your dependency. It would look something like:
dependencies {
implementation 'commons-codec:commons-codec:1.14'
}
This lets Gradle know that you have a dependency to version 1.14 of commons-codec which your codes need to build and run.
This will automatically be downloaded from a remote repository, which you also can specify in your build.gradle file:
repositories {
mavenCentral()
}
This tells gradle to download the dependencies from Maven Central, which probably is the most typical Maven/Gradle repository and most likely hosts most dependencies you would need.
I went through this link to import a gradle project as dependency into another gradle project. Is there a way to include a maven project as dependency into a gradle project?
If that Maven project is built somewhere else and deployed to a Maven repository, you can specify the artifact it produces as a simple compile dependency. If this Maven project is somehow a subproject of a Gradle multi-project build, I suppose you could hack it to work by simply ignoring the Maven POM file and perhaps adding a build.gradle to that project.
To use the solution described on the link that you provided - both projects must be gradle and included in gradle settings. Therefore you can use project closure to compile and depend on the project without building it explicitly.
I am not aware of any way to do this with maven project. I understand you use some maven plugins that you dont want to rewrite in gradle as simply can not find any equivalents etc. Often had that problem.
In this scenario I would suggest to build maven project and depend on a built jar in your gradle project.
Otherwise you could probably amend sourcesets in your gradle project to include maven classes. But I think it would be to complicated.
If I would be you I would turn it into gradle and try to replicate what you had using maven or just build the artifact and depend on it in dependencies closure.
Gradle is not that new anymore and there are many plugins that are superseding old good maven stuff.
Question
I have an internal maven repository in a shared folder say s:\mvnrepo – it's just a convention for a directory layout after all. Now I want to install a library com.example.lib:lib:1.0.0 and its dependencies from another external maven repository into s:\mvnrepo with the hypothetical command
mvn installlib com.example.lib:lib:1.0.0 -Dinto=file:///s:/mvnrepo
That would be a dream! Is such a thing possible?
(I don't have high hopes seeing as everything maven related is always so unapproachable and complicated...)
Supplemental Information
Now, I know there are maven repository management systems like Nexus but I really do think my use case should not require them. I also know about the deploy-file goal but it doesn't install transitive dependencies into the specified repository.
I also thought about simply creating a dummy project that has the specific library listed in its pom.xml. Then just execute mvn install -Dinto=file:///s:/mvnrepo. The problem is that the install goal apparently does not have an option to specify the repository directory (i.e. -Dinto is purely hypothetical).
I found out that it is possible to download a library and all its dependencies with Intellij. That's great but Intellij really only downloads the jars and does not create a maven compatible directory structure (i.e. there are no poms downloaded nor are the jars in a group subfolder etc.).
Background information
For work we want to have an internal maven repository such that it is possible to build an application on a freshly installed server that does not have an internet connection while still being able to specifiy dependencies in the gradle file as if we would use an external maven repository like jcenter (resp. bintray). So the gradle file we use would look something like this:
repositories {
// jcenter() // we don't want that
mavenUrl 'file:///s:/mvnrepo'
}
dependencies {
compile 'com.jakewharton.sdkmanager:gradle-plugin:0.12.0'
}
Now, I downloaded the sdkmanager plugin jar and its pom file from mvnrepository.com and used something like
mvn deploy:deploy-file -Dfile=sdkmanager.jar -Dpom=sdkmanager.pom -Durl=file:///s:/mvnrepo
All fine and dandy, the sdkmanager plugin is installed into s:\mvnrepo with the correct directory layout and gradle even picks it up! But gradle is not quite satisfied, you see. It demands the jarchivelib which is a dependency of com.jakewharton.sdkmanager:gradle-plugin:0.12.0 (see the page on mvnrepository.com where jarchivelib really is listed as a dependency of the sdkmanager plugin). Now, I could repeat the fun and download jarchivelib and its pom from mvnrepository.com, deploy it and so on. Thinking about it... that does not sound like fun at all! So now you see my concrete use case and maybe you could suggest an even better approach than what I seek for in my question.
Hi I am trying to build maven project using intelliJ.
I wanted to know what the maven import option does?
Apart from importing files from local repository and remote maven repository, does it imports/ updates files from SVN repository (which I am using).
So do I need to checkout the code from repository everytime or import option does this ?
Thanks
The maven import functionality in IntelliJ works as follows:
If you want to use an existing Maven project, you can import it directly by opening its pom.xml file. When a Maven project is imported, it maps to an IntelliJ IDEA module with the name, which is equal to the Maven project's artifactId. Dependencies between the Maven projects map to the dependencies of IntelliJ IDEA modules from the libraries and other modules. IntelliJ IDEA analyzes the pom.xml file and automatically downloads the necessary dependencies.
It will not checkout code from your SVN repository. Maven is a build tool that helps manage dependencies that your package may have, while SVN is a version control system that is meant to track changes you make to code.
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).