How to set up a local proxy repository for SBT? - scala

I have near zero knowledge of Maven/Ivy but know that these technologies are behind SBT's ability to download and manage libraries automatically.
When I was trying to generate an IntelliJ Idea project from SBT it (Idea or the generator plug-in) has failed to find cached libraries stored in the common Ivy 2 cache. I've solved the problem by setting up the SBT project to use the old scheme storing downloaded libraries under the project directory. Another project of mine uses old SBT version which does it this way by default. In both cases I use clean command to remove library binaries before backing up and update after to download all the libraries again. Which means I download the same library files many times.
The question is how to set up a local repository to cache the libraries so that SBT update will download the libraries from the local cache if available? Would be nice to use the existing ~/.ivy2/cache directory for the cache if possible.
I tend to believe this is probably fairly simple when you have at least basic Ivy 2 knowledge.

Ivan, this may help.
sbt
> deliver-local
[info] delivering ivy file to /.../target/scala-2.9.1/ivy-1.0.xml
Then, you'll need to find IntelliJ equivalent steps, but in Eclipse you configure the build path with an additional library, "IvyDE Managed Dependencies" offered by the IvyDE plugin
1) browse to the target/scala-version/ivy-1.0.xml file
2) select desired configurations (compile, runtime, test, provided, etc.)
Now you have an ivy representation of your sbt configuration.
This works wonderfully with one annoying exception: on "sbt clean", ivy config file gets blown away and you have to repeat the above. Eclipse project clean preserves ivy config, btw.
Keep posted if this approach can be applied to IntelliJ

Related

Adding Dependency Management to an Existing Java Project

I'm working on upgrading a legacy Java project to be compatible with jboss wildfly. As part of that process, I'm replacing our old system of managing dependencies (manually scanning for jars in a folder) with an automated system.
My first thought was to use maven, which worked well initially. The maven plugin for eclipse was able to scan my project and create a pom with most of the required dependencies. That works fine for compiling and running with eclipse, but production deployment uses an ant build script. I looked into maven-ant-resolver ( https://maven.apache.org/resolver-ant-tasks/index.html ) but as far as I can tell that project doesn't have a way to add dependencies to the classpath, the best it can do is bundle them into a jar.
The other option I looked at was Ivy. It seems better suited to integration with ant. Unfortunately, the tooling for ivy seems primitive compared to maven. From what I can tell, there is no option to generate the dependency file (ivy.xml) from an existing project. With the number of dependencies I'm dealing with, especially from jboss, creating the dependency xml from scratch is not a realistic option.
What are my options for solving this problem? Is there a way to do what I want with maven or ivy that I'm not seeing? Is there another dependency management tool out there that offers all the features I need?
The maven-assembly-pluginis what i can recommend for likely usecases. Not sure if it suits you though.
In a nutshell:
You can pack folders, jars, resources, dependencies, whatever into a jar for production deployment. This jar is packaged with the, from maven-assembly-plugin internally used and thus not needed to be referenced explicitly, maven-archiver-plugin which also stores a MANIFEST.MF with the classpath in it (not by default but with few codes of tweaking).
Useful to know though: Maven allows you to quite easily create own Plugins that completely do what you want. If its just a file with the stored classpath, this could be a clean solution.

Why it took so long to download sbt dependencies when I create a scala project in intellij idea?

I am new to scala, when I create a scala project in intellij it took really so long to download all the jar files. I have installed the scala plugin for idea IDE. Could anyone please tell me what should I do to get on the right track? Thanks very much.
Since the scala language is really a library on top of the JVM, creating a scala project for the first time requires the download of the specified scala version from a maven repository (usually this is at least the compiler and library, which for 2.10 comes to ~20MB). This will happen the first time, even if you've installed that same version of scala on your machine outside of sbt.
Once you go through this the first time, though, the next project you create--whether via sbt on the command line or via an IntelliJ sbt project--will pull these dependencies from your local ivy cache instead.
However, should you change the language version in your project, it will once again have to download the full language dependencies for that version.
In the screenshot it try to download the source files of one of of the dependencies. That mean it will try to download not only the compiled jars, but also it's source and docs.
only after downloading all of them, the project will be ready.
Disable to downloading of the sources and docs, and it will be much faster. How to disable it depends on how you create the project. (eg if you create sbt project, make sure to uncheck the "download sources and docs" in the creation/import wizard)

How does sbt integrate with IntelliJ?

Is there a definite doc somewhere that explains all the magic that happens behind the "Typesafe Activator" generation of "IntelliJ supported" project?
The sbt build files look absolutely monstrous, and I have no idea what and where IntelliJ looks for.
This is frustrating as working from two different PCs the scala seed project refers to different hard-coded paths.
Is there a good place to start?
Last time I checked, the typesafe activator was using SBT as the underlying build tool. When creating an intellij project it would thus use the sbt-idea plugin.
I guess a possible place to start would be that plugin's documentation.
However I think there is something else going on here. I think you have the activator installed on two different PCs and are trying to share the project between both PCs whether using version control or copying the folders.
The sbt-idea plugin will indeed write some absolute path in ideas project files (most likely the absolute paths to the sbt managed libraries in the ivy cache of your home folder) since this is required for the intellij project to work.
There should be no reason to "share" the idea project files, these should be considered computer specific and should not be checked into source control, or expected to work when copied from a random computer to another. You are expected to regenerate them for each computer the project is worked on.
If that sounds like a burden, you may want to install the Intellij scala plugin. Once installed, the sbt integration will allow you to import any sbt project even if you haven't generated the intellij support in the activator. Have a look at the features page, there is a video showing how to use the plugin.

Gradle eclipse classpath - Switching between SNAPSHOT and project dependency

We have a multiple modules in our Java project and each module publishes SNAPSHOT jar files to Nexus repository. All the sub-modules are directly dependent on the SNAPSHOT jar files.
During development, we want to depend on the Eclipse project rather than SNAPSHOT jars. So we introduced a flag which switches between the dependencies as shown below.
if(System.properties.'setupProject'){
compile project(':Core')
compile project(':Module1')
compile project(':Module2')
}else{
compile 'com.test:core:0.1-SNAPSHOT'
compile 'com.test:module1:0.1-SNAPSHOT'
compile 'com.test:module2:0.1-SNAPSHOT'
}
Executing the following command generates the .classpath file as expected.
gradle eclipse -DsetupProject=true
Is there a better way to do this? Can we use Gradle configurations to achieve the same?
I could not find good examples for the same.
At the moment this is the way to go. You might tweak this even more and instead of using a System property to mark a project as available you can check if the project folder is available (project is checked out)
cheers,
René

is there a way to generate a pom.xml with dependencies from an eclipse project?

I have inherited a big project with several subprojects.
all of them use several jar files, all of them located under each project's lib directory. I want to take all the projects and migrate them to maven, but dependencies are a problem (too many of them), some of them are commonly used libraries (apache projects, xerces, jms, etc) and others are not.
is there a way to autogenerate maven dependencies for those jars that can be found on public maven repositories. for example, see that my project use the spice-jndikit-1.2.jar file and automatically get the appropiate depedency with group, artifact and (if possible) version?
thank you
I wrote a groovy script to generate a starting set of Apache ivy files.
https://github.com/myspotontheweb/ant2ivy
In my case, I wanted to "Maven-ize" my ANT builds without switching completely away from ANT.
It is feasible to extend this code to generate a Maven POM, if people were interested in this feature.
You can convert a project to Maven using the m2e plugin, but this erases your jar references, and should not be used.
I doubt that such a thing exists since typical jars (unless themselves built with Maven) don't have the necessary information to correlate the groupId, artifactId and version back to a repository to get the proper path.
You might be able to write something that parses the file name for the name and version, but you still have the package-based path to figure out.
If you're building using Ant, you might also consider using Apache Ivy, and its file-system based resolution (very fast and easy to configure), to get you started, and slowly role over to the Maven repos for the artifacts, this way you're not spending a lot of time up-front finding Maven dependencies.