How create properties (variables) in sbt which are reusable across builds? - java

With maven I can create a parent pom.xml and extends it everywhere. Also in parent pom I can define some general properties like library version and other properties which are wanted to share across several imdepeneded projects. How to do this with sbt if projects are different and they do not build from one root project/folder?
Important
The question is not about multi project build!

Not tested, but should work:
Create a normal SBT project, define your desired settings, etc. there. Let's say it's in the path ~/parent.
Note that the project folder of an SBT project is itself a project.
You can depend on external projects from file system or VCS in SBT. In the builds where you want to reuse parent project, create project/project/Build.scala (or project/build.sbt) if it doesn't exist yet, define the project as usual and and add a dependency on ProjectRef(file("~/parent"), "project").

Related

How to depend on multiple projects in a maven library?

My project depends on an external library, which consists of a number of maven projects.
Do I have to define each of the projects in the library to be a module in my project's parent pom.xml? Is there a way to define the library as a whole in my project without individually listing all the projects?
My project directly depends on only one project in the library, but that project depends on other projects in the library.
Do I need to define all the projects in the library in my project's dependencies?
Ideally, in parent pom you define modules i.e, external libraries which you want to build. When you build the parent pom, it will build all modules which are defined in parent pom. And further, modules will build other dependencies/modules.
You need a composite pom -- a pom which just declares a bunch of dependencies. You depend on it, and transitively, you get its dependencies.
See this discussion for more information.

Is it possible to share build maven profile properties with dependent projects

I have four projects, Proj-A, Proj-B, Proj-C and Proj-D. Proj-B, Proj-C and Proj-D all have Proj-A as a dependency inside their POMs. B,C, and D are not dependent on each other. I have put profiles in Proj-A in order to allow parametrization of some settings (jpa settings for dev, test and prod db connections). That works fine.
Is there a way to build the other projects, and have them pass the proper build profile to use when building the dependent project? If so how? I would like to do this without putting properties in the settings.xml file for the maven repository.
I am familiar with Maven2 but by no means an expert, any ideas?
As far as I know there is no a beautiful way :/
You can inherit profiles from a parent to its children.
But the planned implementation of mixins is still open
What I've seen is that people create a parent containing the profiles and then use it as parent for the project-parent pom.xml
What is also possible is to use a different (released) parent for modules:
project-parent (having modules configured)
/module1 -> using webservice parent pom
/module2 -> using rest parent pom
That is not the usual structure but one way to share configuration for related modules in different projects.

Two Maven project with public module

I have a project that devided into three pieces, PCommon, PWebapp and PAdminConsole. PWebapp and PAdminConsole are dynamic web project in eclipse, PCommon is java project, and both two web project will use the api in PCommon as a jar file in lib folder.
In the past,I added import project in build path, I used Ant to compile and build PWebapp and PAdminConsole and in both build.xml file there is
<ant antfile="${common}/../build.xml" inheritAll="false"></ant>
to make PCommon into a jar file.
Now I will change all my projects to Maven Project. But I don't know how to make two web projects contain one public module, and how to package my PCommon into a jar file automaticlly when I run package maven command to package one web project.
Now I always deploy my PCommon.jar on nexus server. and then add dependcy in pom.xml in web projects. But I think there is no sense to deploy the jar on the public server, so I think it isn't the right way to archieve this goal. Is there any way that is more convenient?
I know I can make a parent project with a parent pom.xml. But I have two web project need the module, the pom.xml in PCommon can only extends one parent, can't it?
The common way to resolve dependencies in Maven is using a repository. The first time a dependency is needed, it is downloaded from your repository and installed in the repository on your local machine. If a dependency is not available in the remote repository it has to be installed to your local one in some other way. There are a few other ways to resolve depenencies without using the repositories but I wouldn't suggest to use the.
To make this a little more convenient, you can use a proper IDE. I use Eclipse with the m2e plugin. It supports something called "workspace resolution", which should be enabled by default. It scans your workspace for other Maven projects before falling back to the repository lookup. This has the advantage that every change you make in your common project is immediatly available in the other projects. I think it also gets installed to your local repository in the background but I'm not sure. Anyway you don't have to worry about it yourself.
Something similar works with IntelliJ IDEA but I don't have that much experience with it. I'm sure Netbeans has some kind of Maven support too.

Dependency between two classes

Is it possible to create something like "dependency" between two classes from different projects in one Eclipse. The point is that in both of this projects I need to have exactly the same class. I'd like to do something which allow me to make a changes in one of them and all the changes will automatically put in second. I hope that I explained it well ;)
I would-
Create a core project
Add core project to Project A
Add core project to Project B
Pretty much like how we use external libraries.
Create your common classes in a separate project. In eclipse set your build path for the other two projects to depend on your common project. When you release your projects, you build your common project as a separate jar and add it to the class path of your other projects.

Migrating from ant to maven in Netbeans

Our software is written in Java and comprise many (7) projects.
These projects are Netbeans ant projects.
I'm considering to converting them to maven2.
Where can I find some hints for doing such thing?
Don't read that book. It will only make you confused. Read this book instead: "Maven - The definitive guide" http://www.sonatype.com/books/maven-book/reference/ .
Also, the maven site has a lot of information, but the structure is terrible so you'll need to use google to navigate in it.
Here is my suggestion:
Do this by hand, not with "automagic" "help" from the IDE. Maven integration doesn't work that good yet, not in any IDE.
Make sure you program project is divided into modules under a common umbrella module, so that each module produces a single binary artifact (jar, war,...) possibly accompanied by the javadoc of the source code behind that artifact, a zip with the source code etc. The basic principle is that each module produces a single artifact, containing all the non-test-code under that module. You can do this while the project is still built by ant.
Each module should conform to the standard maven directory layout. The build destination is under [module]/target/[output-type, e.g. "classes"]. The source code is under [module]/src/main/[src-type e.g. "java"] and [module]/test/[src-type]. The artifact consists of all the code under src/main, and none of the code under src/test, as it built to the target directories. You can do this while the is still built by ant.
Start by transforming the sub-module that has no dependencies on other modules in the project.
Now you can create the parent maven module pom.xml with artifact type "pom", consisting of one of the modules below. Make a child module for the first submodule (the one with only external dependencies), using the umbrella module as "parent". Remember that you need to specify version for the parent. Remember to add the child module as a "module" in the parent too. Always use ${project.version} as version in the child modules when you create multi-module projects like this. All modules under a parent must be released simultaneously in a single operation, and if you use this setting maven will make sure the version fields stay the same across all modules and gets updated everywhere during the release. This may make it difficult to re-use the existing numbering scheme, but that doesn't matter. You are never going to run out of version numbers anyway.
Add the necessary dependencies, and make sure you can build the parent and the child module together using the command "mvn clean install" from the parent module.
Proceed with the rest of the modules the same way. Dependencies to other modules under the same parent project should also use ${project.version} as the "version" they are depending on, meaning "the same version as this". NOTE THAT in order to build, the module you are depending on must be built using "mvn install", so that it gets deployed to you local (computer) repository. Otherwise the depending module will not be able to find the classes. There are NO source-code dependencies between modules in maven, only dependencies to built and packed versions installed in local and remote repositories. This can be very confusing if you come from ant-projects. Build from the root module until you get comfortable with this. It takes two days.
Don't use maven integration in IDEs. It is a bad idea. Use "mvn idea:idea" or "mvn eclipse:eclipse" to set up your workspace as a non-maven ordinary IDE project. The inter-module dependencies mechanisms in maven and the IDE aren't identical and will never be. Also, if you have several mavenized projects with dependencies in between, you want to have several of these in your workspace with dependencies set up between. You can do this with mvn idea:idea / eclipse:eclipse if you create a separate maven project file called "workspace.xml" (or whatever) in the same directory as parent module, set up as a multi-module project containing modules "." and "../otherproject" (only one-way reference here, no parent ref back). If you run "mvn idea:idea / eclipse:eclipse -f workspace.xml" you get a workspace with all these modules linked together. No IDE integration lets you do that. This sound like a lot of extra work, but the workspace.xml-file is really small. It doesn't have to contain all that dependency stuff and all that, only the reference to the modules you want to bind together in your IDE.
I did a succeful migration of NetBeans Ant project to Maven project using the instruccions by Joseph Mocker here: http://forums.netbeans.org/ptopic55953.html
I cite the important part:
close the project
rename the build.xml, nbproject files/folders to something so NB won't recognize them.
close and restart NB (so any memory cache knowledge of the project is gone)
copy in an empty pom from some other project.
open the project back up in NB (NB should now identify it as a maven project)
rearrange the files to follow the maven way (™)
This won't be an easy task since Maven2 expects the files to be organized in a specific way. Anyway Better Builds with Maven is a free book that should get you started. It will help you understand Maven and it also has a chapter on migration.
I discovered that the migration is not necessary. The real requirements that I need was automatic download of dependencies (libraries).
This is also achieved by Ivy which nonetheless uses maven repositories.
I solved converting project from ant to ant+ivy with IvyBeans.
I have built a script to migrate Ant builds to Maven. You can find more information here:
https://github.com/ewhauser/ant2maven
It won't help you with fixing your directory structure and or any additional Ant tasks, but it removes a lot of the tedious steps to get started.

Categories

Resources