Publish SBT project to local directory - maven

I am trying to publish some of my SBT projects on my personal webserver. As far as I know you usually export a SBT project as a Maven directory including a POM.xml, that contains the project definitions.
As Brian Clapper pointed out, you can publish such a Maven repository by creating several configuration files and using sbt publish. In his tutorial, the repository is transferred via FTP.
I want to push my Maven repository to the server manually so I have more control. Can you give me some hints, how to accomplish this?

I figured out how you can do this. This solution creates a local Ivy repository, which is compatible with Maven.
You have to set the following values in your build.sbt:
name := "project-name"
organization := "org.example"
version := "0.0.0"
scalaVersion := "2.9.2"
publishTo := Some(Resolver.file("file", new File("/path/to/your/releases"))
After that, you can publish your release
sbt publish
This will print something like the following lines
[info] Set current project to project-name (in build file:/path/to/your/project/)
[info] Updating {file:/path/to/your/project/}default-2e51ea...
[info] Packaging /path/to/your/project/target/scala-2.9.2/project-name_2.9.2-0.0.0-S
NAPSHOT-sources.jar ...
[info] Resolving org.scala-lang#scala-library;2.9.2 ...
[info] Done packaging.
[info] Done updating.
[info] :: delivering :: org.example#project-name_2.9.2;0.0.0 :: 0.0.0 :: release :: Tue Jul 24 15:41:04 CEST 2012
[info] delivering ivy file to /path/to/your/project/target/scala-2.9.2/ivy-0.0.0.xml
[info] Wrote /path/to/your/project/target/scala-2.9.2/project-name_2.9.2-0.0.0.pom
[info] Packaging /path/to/your/project/target/scala-2.9.2/project-name_2.9.2-0.0.0.jar ...
[info] Done packaging.
[info] published project-name_2.9.2 to /path/to/your/releases/org/example/project-name_2.9.2/0.0.0-SNAPSHOT/project-name_2.9.2-0.0.0.pom
[info] published project-name_2.9.2 to /path/to/your/releases/org/example/project-name_2.9.2/0.0.0-SNAPSHOT/project-name_2.9.2-0.0.0.jar
[info] published project-name_2.9.2 to /path/to/your/releases/org/example/project-name_2.9.2/0.0.0-SNAPSHOT/project-name_2.9.2-0.0.0-sources.jar
[info] published project-name_2.9.2 to /path/to/your/releases/org/example/project-name_2.9.2/0.0.0-SNAPSHOT/project-name_2.9.2-0.0.0-javadoc.jar
[success] Total time: 1 s, completed 24.07.2012 15:41:05
You can put the generated files on any web server (e.g. http://repo.example.org/) and use it in the build script of another project by adding the following lines to your build.sbt:
resolvers += "Personal repository" at "http://repo.example.org/"
libraryDependencies += "org.example" % "project-name" % "0.0.0"
For more information, see SBT: Getting Started Library Dependencies and SBT: Publishing.

From sbt, you can use
project myproject
[myproject] $ publish-local
Which will publish to your local ivy directory (usually ~/.ivy2/local).
In the output you will see the paths of all the files:
[info] Done packaging.
[info] published myproject_2.9.1 to .../ivy2/...myproject.../poms/myproject_2.9.1.pom
[info] published myproject_2.9.1 to .../ivy2/...myproject.../jars/myproject_2.9.1.jar
[info] published myproject_2.9.1 to .../ivy2/...myproject.../srcs/myproject_2.9.1-sources.jar
[info] published myproject_2.9.1 to .../ivy2/...myproject.../docs/myproject_2.9.1-javadoc.jar
[info] published ivy to .../ivy2/...myproject.../ivys/ivy.xml
Then you can grab those files and upload them to your ftp server.
I would still recommend the approach described in the linked blogpost though. At least that's how we do it. Just a small note on storing credentials. Use the following sbt setting:
val credentials = Credentials(Path.userHome / ".ivy2" / ".my-credentials")
The credentials file will look like this:
realm=Sonatype Nexus Repository Manager
host=nexus.example.com
user=deployment
password=pass
The credentials are the same you use for logging in to the Nexus web interface.

Related

Jenkins configuration to download jar file from Maven central and put it in Nexus

I have an environment and we skipped to download all the dependencies from Maven central. So we need to create a Jenkins job and it has a parameter with detail for which component jar it should download from central, and if we provide that details it should download and it needs to be put in a particular repository in Nexus.
I'm totally new to this environment, can someone provide inputs of any plugins to use or some scripts to be used ? Thanks in advance !
Unfortunately that is not so trivial as it might seem. At first glance there are two plugins which might help:
maven-dependency-plugin - may download artifact from repository
maven-deploy-plugin - may upload artifact to repository
however that won't work due to a "couple" of reasons:
artifacts have dependencies - that is not enough to transfer single artifact and tell developers something like "well, now you may use it" - you need to traverse all transitive dependencies and transfer them as well
artifacts have classifiers - most useful of them are javadoc and sources, and if artifact has those useful classifiers it is a good idea to transfer them as well
artifact descriptor (pom) may refer to another (parent) pom - there is a dedicated cauldron in the hell for developers who do not flatten poms when publishing artifacts, however we can do nothing with that - we need to transfer parent poms as well
dependencies may have snapshot or range versions - there is another dedicated cauldron in the hell for developers who use such dependencies
and the reasonings mentioned above lead us to the idea that we need a specialised software (read: basic shell scripting won't work) which is capable to transfer consistent copy of artifact from one repository to another. And all the time I did believe the guys, who are responsible for such decisions (create own trusted repository and manage it), do have idea how to do that properly, unfortunately it seems that they don't.
I have tried to implement some PoC using maven API which is capable to transfer consistent copies (transitive dependencies, pom and parent poms) of artifacts across repositories, hope that will help.
% mvn tel.panfilov.maven:reposync-maven-plugin:0.1.0:single \
-Dartifact=org.springframework:spring-tx:5.3.22 \
-DsourceRepositories=central::default::https://repo.maven.apache.org/maven2 \
-DtargetRepository=local::::http://localhost:8081/repository/maven-releases \
-Dtransitive=true \
-DdryRun=true \
-DsyncSources=true \
-DsyncJavadoc=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- reposync-maven-plugin:0.1.0:single (default-cli) # standalone-pom ---
[INFO] Processing Dependency {groupId=org.springframework, artifactId=spring-tx, version=5.3.22, type=jar}
[INFO] Source repositories: [central (https://repo.maven.apache.org/maven2, default, releases+snapshots)]
[INFO] Target repository: local (http://localhost:8081/repository/maven-releases/, default, releases+snapshots)
[INFO] Discovered 16 artifacts
[INFO] org.springframework:spring-beans:jar:5.3.22
...
[INFO] org.springframework:spring-tx:pom:5.3.22
[INFO] Found 16 missing artifacts
[INFO] org.springframework:spring-beans:jar:5.3.22
...
[INFO] org.springframework:spring-tx:pom:5.3.22
[INFO] Dry run, exiting
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

How to avoid the deploy of sources from Jenkins (to Nexus) in deploy phase?

I have a Jenkins+Nexus installation, and some project there that are automated.
Jenkins deploy the sources and javadocs to nexus in the deploy phase (I am using Maven Release) -- Javadoc could be ok but I don't want to have any sources in Nexus.
I was searching and googling and I don't know how to skip this step.
Thanks a lot
Output from jenkins
[INFO] [INFO]
[INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) # projectA ---
[INFO] [INFO] Installing /var/lib/jenkins/jobs/MyBuild/workspace/target/checkout/MyProject/projectA/target/projectA-1227.jar to .../projectA-1227.jar
[INFO] [INFO] Installing /var/lib/jenkins/jobs/MyBuild/workspace/target/checkout/MyProject/projectA/pom.xml to .../projectA-1227.pom
[INFO] [INFO] Installing /var/lib/jenkins/jobs/MyBuild/workspace/target/checkout/MyProject/projectA/target/projectA-1227-sources.jar to .../projectA-1227-sources.jar
[INFO] [INFO] Installing /var/lib/jenkins/jobs/MyBuild/workspace/target/checkout/MyProject/projectA/target/projectA-1227-javadoc.jar to .../projectA-1227-javadoc.jar
[INFO] [INFO]
Try adding : -Darguments="-Dsource.skip=true -Dmaven.javadoc.skip=true"
to your maven release plugin config (Jenkins > your build configuration > Maven release build > Release goals and options
By default this actually does not happen automatically. Most likely you have configured a release profile somewhere in your pom hierarchy that adds the Maven Source Plugin execution.
You could remove that if you never want to use it or otherwise change the release plugin config to use a different profile or skip source creation or invoke the build with parameters to skip as Guilame has answered

Jenkins Build fails while compiling java code using Maven

I have a Java application and I am able to compile it using maven on CLI, I have a couple of build scripts to deploy the Java code to a remote server and compile the code using Maven but unfortunately the build fails while compiling it using my build script for Jenkins. When I try to compile the code on the remote server the build is successful. The error log is as follows:
[WARNING] The POM for org.im4java:im4java:jar:1.3.2 is missing, no dependency information available
[WARNING] The POM for com.aliyun:aliyun-openservices:jar:1.0.0-20120705 is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] GuestPath Project ................................. SUCCESS [5.183s]
[INFO] guestchat biz module .............................. FAILURE [4.001s]
[INFO] GuestChat Web Common .............................. SKIPPED
[INFO] GuestChat Portal Webapp ........................... SKIPPED
[INFO] guestchat openapi Webapp .......................... SKIPPED
[INFO] guestchat chat server ............................. SKIPPED
[INFO] guestchat service dashboard Webapp ................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.518s
[INFO] Finished at: Wed Sep 19 10:49:39 CST 2012
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project guestchat-biz:
Could not resolve dependencies for project com.guestops.guestchat:guestchat-biz:jar:1.0.0-SNAPSHOT:
The following artifacts could not be resolved:
org.im4java:im4java:jar:1.3.2, com.aliyun:aliyun-openservices:jar:1.0.0-20120705:
Failure to find org.im4java:im4java:jar:1.3.2 in http://repo.maven.apache.org/maven2
was cached in the local repository, resolution will not be reattempted until
the update interval of central has elapsed or updates are forced -> [Help 1]
I hope I am being clear enough, any help is highly appreciated. I can provide with more details if needed..Thanks in advance!
I think you should try to do the following:
Try to compile the project locally but make sure you're downloading the artifacts from the maven repository. Radically you can do it by renaming your ~user/.m2/repository and running ''mvn clean install''
If it compiles - your local maven is configured properly and its not a pom.xml issue. Also the remote repositories are configured right in maven. In this case its a jenkins issue or rather the issue of maven installed on the same machine with jenkins or maybe some network/security related issue (like a connection to the remote repository is firewalled and not accessible from the jenkins machine and so on).
I would suggest to check out the repository configuration on that maven, or if you want checkout the source code of your project on the jenkins machine and manually run the first step described above from that machine. You should see that the code can't be compiled and get the same error you're getting now.
Now if in during the step 1 the project can't be compiled - its just because you have had a dependency on the lacking module and they were somehow presenting in your local repository (I assume previously it could be compiled locally) and since we're kind of running a clean installation the local repository 'gets purged'. In this case you should find out where do your dependencies come from.
Things like
mvn dependency:tree on the failing module
Can be helpful here.
Hope this will help somehow
2 possible quick solutions:
If it compiles locally, then go to the build configuration on Jenkins and check the "Poll SCM" option to do polling on every build.
Run from the command line on Jenkins server mvn clean install and make sure that the artifacts are available in maven repository after the build. If they are not, download the jars of the relevant versions and put them in the local maven repository (on Jenkins server).
Good luck!
This is what the error message tells you:
The following artifacts could not be resolved:
org.im4java:im4java:jar:1.3.2,
com.aliyun:aliyun-openservices:jar:1.0.0-20120705: Failure to find
org.im4java:im4java:jar:1.3.2 in http://repo.maven.apache.org/maven2
So you have references in your pom for im4java 1.3.2 and aliyun-openservices 1.0.0-20120705, but these cannot be found in the general maven repo. If you do a search for im4java in mvnrepository.com, you'll see that it only has version 1.2, so it is reasonable that it would fail. aliyun-openservices package doesn't exist there at all.
So, where should those be found? Maybe you have installed them locally only and not to Jenkins machine?
What you can do then, is either
(recommended) set up an intranet maven repo where you would deploy the needed libraries and reference that or
install them locally on the machine running Jenkins
Both of these should fix the issue.
A quick look on maven repository, only version 1.2.0 is available and not version 1.3.2 (which you are using).
Does your code compile with a maven build?
I suggest you try to review the repository setting on the jenkins instance

maven deploy-file failed with 503: cannot find maven-metadata.xml

I am using Maven to integrate our project with others, maybe in a unpopular way and encountered issue.
We have a project that used to compile with Ant. Ant script is big and awesome, so when we are using Maven for integration, it is decided to keep compiling with Ant.
Now let's say Ant compile output is res-1.0-SNAPSHOT.tar.gz. (any filename can be possible but it IS tar.gz) And I am deploying the file to a nexus-hosted snapshot repository called "snapshots".
I tried to deploy with this command:
mvn deploy:deploy-file \
-DgroupId="com.my-company" \
-DartifactId="res" \
-Dversion="1.0-SNAPSHOT" \
-Dpackaging="tar.gz" \
-Dfile="res-1.0-SNAPSHOT.tar.gz" \
-Durl="http://our-nexus-ip/nexus/content/repositories/snapshots" \
-DrepositoryId="snapshots"
I have a simple settings.xml in ~/.m2 with proxy and server settings. However server settings is not being used in current progress yet, wrong passwords don't get errors.
The output is like this:
[[root#cnbi maven]# ./run.sh
+ mvn deploy:deploy-file -DgroupId=com.my-company -DartifactId=res -Dversion=1.0-SNAPSHOT -Dpackaging=tar.gz -Dfile=res-1.0-SNAPSHOT.tar.gz -Durl=http://135.252.234.142:8081/nexus/content/repositories/snapshots -DrepositoryId=snapshots
Warning: JAVA_HOME environment variable is not set.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'deploy'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [deploy:deploy-file] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [deploy:deploy-file]
[INFO] Retrieving previous build number from snapshots
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error retrieving previous build number for artifact 'com.my-company:res:tar.gz': repository metadata for: 'snapshot com.my-company:res:1.0-SNAPSHOT' could not be retrieved from repository: snapshots due to an error: Error transferring file
Server returned HTTP response code: 503 for URL: http://135.252.234.142:8081/nexus/content/repositories/snapshots/com/my-company/res/1.0-SNAPSHOT/maven-metadata.xml
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 51 seconds
[INFO] Finished at: Tue Jun 12 08:44:13 CST 2012
[INFO] Final Memory: 7M/209M
[INFO] ------------------------------------------------------------------------
You see, it fails to find maven-metadata.xml. It is for certain, the file and its folder "com/my-company" do not exist at all in the repository.
Besides any misuse of Maven you may find, my questions are:
1) am I using Maven in the right way? (deploying tar.gz, using deploy:deploy-file...)
2) are there incorrect parameters?
3) what is maven-metadata.xml for? It is not there but Maven insists to find it -- I guess it is generated, am I missing some steps?
The solution might be stupid, I am really not familiar with Maven. Unfortunately it has to be done... Please, help me out of this.
Maven version is 2.0.11. Let me know if you want to know more.
I had the same problem and there was a bug in our nexus.
Using maven3 (with same settings.xml, pom.xml and .m2 repo) solved our problem and its easier than upgrading nexus.
You can check this bug also.
If you're still stuck with using Ant, I would recommend you at least consider adding Ivy to the picture, as your dependency manager. If I recall correctly, it was able to update maven-metadata.xml files in the repository.
Have a look at this example.

Transitively download a Maven artifact to the local repository

I am trying to download a specific artifact (and all of its dependencies) to a machine's local repository.
It would seem that using the dependency:get goal would be the best option for this, but despite the documentation it does not seem to actually get the transitive dependencies.
Here is an example where I have tried to use dependency:get to download the spring-core jar and all of its many dependencies. You'll notice that the spring-core jar is the only thing downloaded despite the fact that this was done after cleaning the local repository.
$ mvn org.apache.maven.plugins:maven-dependency-plugin:2.2:get -DrepoUrl=http://repo1.maven.org/maven2/ -Dartifact=org.springframework:spring-core:3.0.5.RELEASE -Dtransitive=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.2:get (default-cli) # standalone-pom ---
Downloading: http://repo1.maven.org/maven2/org/springframework/spring-core/3.0.5.RELEASE/spring-core-3.0.5.RELEASE.jar
Downloaded: http://repo1.maven.org/maven2/org/springframework/spring-core/3.0.5.RELEASE/spring-core-3.0.5.RELEASE.jar (374 KB at 548.4 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.401s
[INFO] Finished at: Wed May 25 00:29:47 CDT 2011
[INFO] Final Memory: 7M/107M
[INFO] ------------------------------------------------------------------------
My questions are:
Is this a bug with the dependency:get goal?
If not, what am I doing wrong?
Are there any alternatives methods I could use to accomplish my initially stated goal?
If this is a one time or irregular occurrence for you, The simplest thing to do would be to define the dependency in a POM and run mvn package or similar to retrieve the dependency artifacts. You could also try mvn dependency:sources if you'd like to have the source jars too.
If this is something you want to do more regularly or as part of a process, you could look at using Aether directly to retrieve the dependencies for you.
Another approach if this is something you need to do regularly to manage groups of artifacts into your internal development ecosystem is to use Nexus' procurement suite to retrieve the dependencies and manage them into your repository.
You might can go with this solution
1) Download the artifact as you described (I tested with version 2.5.2)
c:\test>mvn -DrepoUrl=http://repo1.maven.org/maven2/ -Dartifact=org.springframework:spring-core:2.5.2 -Dtransitive=true
2) Download the pom (-Dpackaging=pom) of this artifact
c:\test>mvn -DrepoUrl=http://repo1.maven.org/maven2/ -Dartifact=org.springframework:spring-core:2.5.2 -Dtransitive=true -Dpackaging=pom org.apache.maven.plugins:maven-dependency-plugin:2.2:get
3) Use the downloaded pom to copy all dependencies via the dependency:copy-dependency gaol
c:\test>mvn -DoutputDirectory=C:/test/dependency -f C:/<path-to-repository>/org/springframework/spring-core/2.5.2/spring-core-2.5.2.pom dependency:copy-dependencies
You will find the dependencies (including test and optional scope!) in the created c:\test\dependency folder. To exclude test and optional scope use -DincludeScope=runtime.
You need to dynamically build some path information (e.g. path to the pom in your repository) to set up this solution and also need to bring the artifact itself together with its dependencies but it should work in a script without generating a special pom (which might be easier).
It would appear the answer to question #1 (Is this a bug with the dependency:get goal?) is yes. As of 5/25/2011 issue MDEP-308 is still unresolved.

Resources