Compare and Merge Source Code Files in Java

Compare and Merge Source Code Files in Java

As a Java developer, if you are going to build a source code comparison tool or application or you may need to manage several versions of code files then you may think about how to compare and merge source code files in Java. If so, this article will explain how easily we can do this without writing a number of lines of code. Besides, we shall see how we can retrieve, accept or discard the changes while merging the source code files.

This article will cover the points given below:

Java library to compare and merge source code files

To compare and merge source code files, we shall use Java code comparison library which consists of all important and outstanding source code comparison features. This Java code comparing library can either be downloaded or can be installed directly by using the Maven configuration as given below.

<repository>
    <id>groupdocs-artifacts-repository</id>
    <name>GroupDocs Artifacts Repository</name>
    <url>https://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-comparison</artifactId>
    <version>22.11</version>
</dependency>

How to compare and merge source code files in Java

Suppose we have two files (source.cs and target.cs) of different versions of a source code as shown in the following screenshot.

Source code files to compare

By considering the files above, let’s write a code snippet in Java by following the steps as given below:

  1. Initialize Comparer class with the source document’s path or stream.
  2. Invoke add() method and specify target document’s path or stream.
  3. Invoke compare() method.

The following code sample shows how easily you can compare and merge source code files in Java.

//Instantiate Comparer object with source document path or stream;
try (Comparer comparer = new Comparer(SourceFile)) {
// Invoke add method and specify target document path or stream.
comparer.add(TargetFile);
//Invoke compare method.
final Path resultPath = comparer.compare(ResultPath);
}

As a result, we shall get the merged source code file and an HTML file highlighting the changes in the code. Please note that the deleted elements will be marked in red, the added – in blue, and the modified – in green.

Compare and merge source code files

Get the list of changes using Java

To get the list of all changes after comparison, we shall write the code as per the following steps:

  1. Initialize Comparer object with source file’s path.
  2. Invoke add() method and specify target file’s path.
  3. Invoke compare() method.
  4. Get the list of changes by using getChanges() method.

The code sample below demonstrates how to get the list of all changes.

//Initialize Comparer object with source file path
try (Comparer comparer = new Comparer(sourceFile)) {
//Invoke [add][] method and specify target file path.
comparer.add(targetFile);
//Call compare method
final Path resultPath = comparer.compare();
//Get the list of changes by using getChanges method
ChangeInfo[] changes = comparer.getChanges();
//Display the changes count
System.out.println("Count of changes: " + changes.length);
}

Advance options in Java code comparison library

If you need to accept or reject several versions of code, you may have to apply or discard the changes. In this case, please follow the steps given below:

  1. Initiallize object of Comparer class with the source document’s path or stream.
  2. Call add() method and set target document’s path.
  3. Invoke compare() method.
  4. Invoke getChanges() method and obtain detected changes list.
  5. Set ComparisonAction of needed change object to ComparisonAction.ACCEPT or ComparisonAction.REJECT value.
  6. Call applyChanges() method and pass collection of changes to it.

The following code snippet shows how to use the advanced options of the code comparison library.

//Initialize Comparer object with source file path
try (Comparer comparer = new Comparer(sourcePath)) {
//Invoke [add][] method and specify target file path.
comparer.add(targetPath);
//Call compare method
final Path resultPath = comparer.compare(outputPath);
//Get the list of changes by using getChanges method
ChangeInfo[] changes = comparer.getChanges();
//Set ComparisonAction of needed change object to ComparisonAction.ACCEPT value.
for (int i = 0; i < 10; i++) {
changes[i].setComparisonAction(ComparisonAction.ACCEPT);
}
//Set ComparisonAction of needed change object to ComparisonAction.REJECT value.
for (int i = 10; i < changes.length; i++) {
changes[i].setComparisonAction(ComparisonAction.REJECT);
}
// Call applyChanges method and pass collection of changes to it.
comparer.applyChanges(resultPath, new ApplyChangeOptions(changes));
}

Get a Free License

You can get a free temporary license to try the API without evaluation limitations.

Conclusion

To sum up, we hope that you have learned how to compare and merge source code files programmatically using a Java code comparison library. As a result, you have got the summary of inserted or deleted content. Furthermore, you have gone through the ways to get the changes list and accept or reject the identified changes.

You may visit the documentation to explore other features.

Ask a question

You can let us know your questions or queries on our forum.

FAQs

How to compare and merge source code files in Java?

Initialize Comparer class with source document path or stream, call add() method, and set the target document’s path or stream. After that, invoke compare() method of Comparer class.

See Also