aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes How to Compile source code from a different Java project using Maven Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "How to Compile source code from a different Java project using Maven" Watch "How to Compile source code from a different Java project using Maven" New topic
Author

How to Compile source code from a different Java project using Maven

Ravish Srivastava
Greenhorn

Joined: Sep 05, 2011
Posts: 23
Hi,

I have a requirement as below:

1. I have a project B and when i build this , it refers java class from a differnet java project say A.
2. First it start compiling project A source code and then start building source code in B.
3. While making jar file for B, it has classes from Project A as well As project B.

B.jar = A (source code) + (B source code ).

This I was able to successfully achieve by Build.xml.

How can we do the same using Maven?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

Maven projects are supposed to be self-contained. This is both a virtue and a limitation as compared to Ant. They also are supposed to emit a single product (such as a jar), not a compound (such as a directory of classes).

The equivalent in Maven would be for the first project to build and install a jar into the Maven repository. The second project would then reference that jar. Since you want to merge the two sets of classes, you'd have to provide a mechanism for unpacking the classes from the first project into the working directory that will be jar'ed up to build the second jar. It would probably have to be done as a Maven anttask.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ravish Srivastava
Greenhorn

Joined: Sep 05, 2011
Posts: 23
Hi Tim,

how can we specify in Maven, if I want only to compile and archived , only set of java source files which are placed in some other project like below?

project A:

my.pack1.A1.java
my.pack1.A2.java
my.pack1.A3.java
my.pack2.B1.java
my.pack2.B2.java
my.pack2.B3.java

Project B:

-- Want to refer the source file present in project A while compiling project b source code. So that my code dependency of source a can be resolved at compile time.

Thanks
RS

Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

I am not sure I understand you question. In Maven, if you have two different projects, you have two different directory structures. Thus you would have:





And if projectB depends on projectA, then in the pom.xml for projectB place a dependecy on projectA.

If you want to ensure that while editing projectB, that the source for projectA is available, indicate in the pom.xml for projectA that you also want the sources JAR to be build and uploaded to the repository. This should help: http://maven.apache.org/plugin-developers/cookbook/attach-source-javadoc-artifacts.html. As long as the sources JAR is in the repository, and you configure your IDE to download the sources JAR, then the sources will be available.

JBoss In Action
Ravish Srivastava
Greenhorn

Joined: Sep 05, 2011
Posts: 23
Hi All,

I am trying to ask in detail once again :
Below are my two different project A & B

Project A
+ pom.xml
+ src
+ main
+ java
+ my
+ packA1
+ A1.java
+ A2.java
+ A3.java
+ packA2
+ A4.java
+ A5.java
+ A6.java


Project B

+ pom.xml
+ src
+ main
+ java
+ my
+ packB1
+ B1.java
+ B2.java
+ B3.java


Classes in project B have dependency in project A source files. I am able to compile successfully by giving the Project A as the dependency to Project B's POM.xml.

Now, I have to put all the compiled source files from Project B and some compiled source file from Project A (which are referenced by source files in B) in a common folder (say "common" ) in proper package structure, so that i can make a jar out of this folder.

common
+ my
+ packB1
+ B1.class
+ B2.class
+ B3.class
+ packA1
+ A1.class
+ A2.class
+ A3.class


Please help me to resolve my problem in Maven.

Thanks & Regards,
RS




Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

No, your classes in Project B don't have a dependency on Project A "source files".

Java is not like C/C++ where there are conditional compilation options. In Java, a class is a class is a class. About the only difference worth mentioning is when you'd be targeting 2 different runtime JVM major versions, which is not something we'd recommend anyway.

So the source files don't really matter as long as you have the class files that come from compiling the source. Especially in Maven, which is more into discrete versions of products.

That is why the best way to create a jar that includes classes from some other project is to have that other project produce a jar which the second project then unpacks and repacks into the second jar.
Ravish Srivastava
Greenhorn

Joined: Sep 05, 2011
Posts: 23
Hi Tim,

My point for dependency was like below:

package my.packB1;

import my.packA1.A1;
import my.packA1.A2;
public class B1{

A1 a1 = new A1();
int i = A2.someMethod();

}
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

I know. Still don't need the source code for A in B. Just the classes.
Ravish Srivastava
Greenhorn

Joined: Sep 05, 2011
Posts: 23
My bad,

not the source code from the Project A, only the .class files from project A

Thanks
RS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Compile source code from a different Java project using Maven