0

I'm a Java developer using Eclipse and Maven. There are some modules I created in a project that I'd like to reuse in my future projects. Is there any standard way to accomplish this using Maven?

I don't like the idea of including classpath in other project. I'm looking for a way to publish and share some specific packages using Maven and over the time I should have a consistent Java library for myself.

Shuo
  • 4,749
  • 9
  • 45
  • 63
  • 1
    With Eclipse, you can simply right-click your project and select `Export`, select Java Jar, select the resources you want, and you are done. You can then add that jar to the classpath/buildpath of any of your other apps to reuse the classes within. – Sotirios Delimanolis Feb 20 '13 at 19:39

4 Answers4

3

You can publish those already existing jars in your repo using mvn clean package install and then add those as a dependency to your pom file in the new projects, whereever you chose to use them

happybuddha
  • 1,271
  • 2
  • 20
  • 40
  • I don't have any jars. What I have is several source files that I want to reuse. – Shuo Feb 20 '13 at 20:07
  • So what did you mean when you said > There are some modules I created in a project....How did you create those 'modules'? Do your classes have a package statement on the top ? – happybuddha Feb 20 '13 at 20:10
  • Yes the classes are different packages. But these packages are not in jars. – Shuo Feb 20 '13 at 20:11
  • By "module" I mean a set of classes that work collectively for some purpose. You can think of it as a particular set of source files – Shuo Feb 20 '13 at 20:16
  • I am not sure how you can accomplish this using maven. If you want to reuse a bunch of classes you can a) Either jar them up and install in your repo. b)Copy src/class folders into a seperate folder on windows and whenever you begin a new project, create a folder structure which is inline with the package statement in your util classes and then paste those src/class files. Does this answer your question ? – happybuddha Feb 20 '13 at 20:19
2

This is a very common usecase and nicely covered as maven multimodule project. Check the link for a simple example http://docs.codehaus.org/display/MAVENUSER/Multi-modules+projects. For local development it's enough to run mvn install. In a bigger project or shared environment you will need a maven repository.

Update:

Alternative link: Multi-module project builds with Maven and Gradle

kofemann
  • 4,217
  • 1
  • 34
  • 39
1

If you want to reuse some code, then you would have to make a new artifact and add this one as a dependency to your over deliverables. As mentioned by tigran this could be nicely done by using maven multi modules architecture but you can just create a new deliverable used other several projects.

benzonico
  • 10,635
  • 5
  • 42
  • 50
0

If you can build a jar for the classes you want to reuse, you can deploy them to a Maven repository for future reuse by other projects. However, based on the comments I read so far, you want something like source code level sharing. I assume you use Windows platform for your development. You can use junction or symbolic link to link at the source code level between different projects. Also most of modern version control system like Accurev/Clearcase have support for symbolic link.

Lan
  • 6,470
  • 3
  • 26
  • 37