天天看點

編譯zookeeper源碼

一般學習 某個架構基本都是從編譯源碼開始的, 于是那就來吧

首先先clone 官方的源碼

https://github.com/apache/zookeeper.git

放到idea 編譯

mvn clean package -Dmaven.test.skip=true
           

這個時候發現抱錯了

編譯zookeeper源碼

為什麼有個git.properties 比較奇怪

看報錯的原因,似乎是有部分代碼使用了resources\git.properties檔案的配置,但是我們找了一下,在github上的代碼中不會存在git.properties,那麼是哪裡出問題了呢?我們來到pom中找到了

properties-maven-plugin

插件,看到了如下的配置:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <configuration>
              <files>
                <file>${basedir}/src/main/resources/git.properties</file>
              </files>
            </configuration>
          </execution>
        </executions>
      </plugin>
           

原來是這裡初始化的時候會加載該配置檔案的内容。

我們可以先注釋掉它然後繼續編譯 發現還是報錯了

[INFO] --- exec-maven-plugin:1.6.0:exec (generate-version-info) @ zookeeper ---
Usage:  java  -cp <classpath> org.apache.zookeeper.version.util.VerGen maj.min.micro[-qualifier] rev buildDate outputDirectory[ERROR] Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:804)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:751)
    at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:313)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for Apache ZooKeeper 3.7.0-SNAPSHOT:
[INFO]
[INFO] Apache ZooKeeper ................................... SUCCESS [  4.962 s]
[INFO] Apache ZooKeeper - Documentation ................... SUCCESS [  3.308 s]
[INFO] Apache ZooKeeper - Jute ............................ SUCCESS [ 16.448 s]
[INFO] Apache ZooKeeper - Server .......................... FAILURE [  2.057 s]
[INFO] Apache ZooKeeper - Metrics Providers ............... SKIPPED
[INFO] Apache ZooKeeper - Prometheus.io Metrics Provider .. SKIPPED
[INFO] Apache ZooKeeper - Client .......................... SKIPPED
[INFO] Apache ZooKeeper - Recipes ......................... SKIPPED
[INFO] Apache ZooKeeper - Recipes - Election .............. SKIPPED
[INFO] Apache ZooKeeper - Recipes - Lock .................. SKIPPED
[INFO] Apache ZooKeeper - Recipes - Queue ................. SKIPPED
[INFO] Apache ZooKeeper - Assembly ........................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  27.256 s
[INFO] Finished at: 2020-03-06T18:35:56+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (generate-version-info) on project zookeeper: Command execution failed.: Process exited with an error: 1 (Ex
it value: 1) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

           

我們還注意到一個插件exec-maven-plugin,裡面使用了部分參數在項目中并不存在,應該是引用了git.properties的配置參數導緻了pom報錯,導緻初始化編譯失敗,如下:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>generate-version-info</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <workingDirectory>${project.basedir}/src/main/java/</workingDirectory>
              <executable>java</executable>
              <arguments>
                <argument>-classpath</argument>
                <classpath />
                <argument>org.apache.zookeeper.version.util.VerGen</argument>
                <argument>${project.version}</argument>
                <argument>${git.commit.id}</argument>
                <argument>${build.time}</argument>
                <argument>${project.basedir}/target/generated-sources/java</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
           

我們可以使用一個固定值來替代

git.commit.id

build.time

<argument>ccccccccccc</argument>
                <argument>2020-03-01</argument>
           
編譯zookeeper源碼

再來一次欸發現可以于是大功告成終于邁出了第一步 [苦笑]