简体   繁体   中英

Git: maintain 2 branches with 99% of similar code

I'm working on a Java project with Maven, where I need to deploy very often to two different weblogic environments. The only differences between these 2 deployments are a bunch of changes in these 2 files: pom.xml and weblogic.xml files. The rest of the files are the same.

We're creating 2 GIT branches for this: dev & parallel-dev .

But we're having many problems maintaining and merging the changes between these two branches.

All the changes are done in parallel-dev branch, and once this code is reviewed and approved, we're merging it to dev branch, except those two files that don't need to be merged (unless the pom version is modified - in that case we need to merge just the version change but not the rest of changes in pom.xml). This is kind of messy.

I think this method is pretty confusing and can be improved, but can't really see how. I'd love to just keep one branch for this process and avoid all those crazy merges that we're facing.

Any advice would be much appreciated.

-- EDIT --

The difference in the pom.xml is just a different profile for parallel-dev branch:

<profiles>
<profile>
  <id>parallel-dev</id>
  <activation>
    <activeByDefault>false</activeByDefault>
  </activation>

  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
      <resource>
        <directory>src/test/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>env.properties</include>
          <include>more.properties</include>
          ...
        </includes>
      </resource>
    </resources>

    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
        <filtering>true</filtering>
        <excludes>
          <exclude>env.properties</exclude>
          <exclude>more.properties</exclude>
          ...
        </excludes>
      </testResource>
    </testResources>
  </build>
</profile>

In weblogic.xml the difference is the context-root name and a library reference (this lib also has parallel-dev and dev branches).

I would do the following:

  1. Isolate the changes you made to those two files in the dev branch.
  2. Destroy the dev branch and recreate it from the parallel-dev branch.
  3. Reapply the changes on the dev branch in a new commit.

Now, whenever new development happens on the parallel-dev branch, switch to the dev branch and do:

git rebase parallel-dev

(or the equivalent operation in your GUI tool). This will reset the dev branch to the newest version of parallel-dev and reapply the isolated change to those two xml files.

If the change to parallel-dev happened to overlap with the isolated changes you will still have merge conflicts, but otherwise you should be good to go. (Although I would double-check the .xml files just to be safe)

I would create two Maven submodules in your project, keeping the code in the parent module but the different configurations in the submodules. This way you don't need two different branches, and so you get rid of all those related merging conflicts.

Relevant part of parent POM:

<modules>
    <module>dev</module>
    <module>paralleldev</module>
</modules>

And in the sub-modules you just need to create the specific POM and the src/main/resources folder and in the POM include the sources from the parent. The submodule-relevant part:

<build>
    <sourceDirectory>../src/main/java</sourceDirectory>
</build>

You can do even more with the Maven Compiler Plugin—take a look here: Guide to Using Maven when You Can't Use the Conventions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM