FlatBuffers 빌드 자동 구성
Development/Netty & FlatBuffers

FlatBuffers 빌드 자동 구성

반응형

Flatbuffer 빌드 자동화


  • https://github.com/davidmoten/flatbuffers 를 사용하자
    • 빌드할 때 자동으로 flatbuffer executable 파일을 생성해줌과 동시에 해당 executable을 실행시켜서 메세지를 클래스로 변환을 자동화
  • profile에 따라 OS별 다른 executable을 실행하도록 구성
    • OS는 크게 세가지(Unix, Linux, Windows)로 구분되며 각 실행시킬 수 있는 파일이 다르다.
    • Unix(Mac)의 경우 unix의 CMake로 빌드를 해야 하며
    • Linux의 경우는 Make로 빌드
    • Windows는 release 되는 exe 로 빌드해야 함
  • maven plugins를 이용해서 플랫버퍼 컴파일러를 컴파일
  • 지정한 path에 해당 executable 파일을 실행시켜서 클래스를 생성(java)



    <properties>
        <fbs.sources>${basedir}/src/main/fbs</fbs.sources>
        <fbs.generated.sources>${basedir}/src/main/java</fbs.generated.sources>
        <fbs.packet.directory>${basedir}/src/main/resources/packet/packet.fbs</fbs.packet.directory>
    </properties>

    <!-- for getting os type in profiles -->
    <profiles>
        <profile>
            <id>Windows</id>
            <activation>
                <os>
                    <family>Windows</family>
                </os>
            </activation>
            <properties>
                <bin.path>${basedir}/bin/flatc.exe</bin.path>
            </properties>
        </profile>
        <profile>
            <id>mac</id>
            <activation>
                <os>
                    <family>mac</family>
                </os>
            </activation>
            <properties>
                <bin.path>${basedir}/bin/flatc_mac</bin.path>
            </properties>
        </profile>
        <profile>
            <id>unix</id>
            <activation>
                <os>
                    <family>unix</family>
                    <name>Linux</name>
                </os>
            </activation>
            <properties>
                <bin.path>${basedir}/bin/flatc</bin.path>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!-- for make flatbuffer build automatically-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.github.davidmoten</groupId>
                                    <artifactId>flatbuffers-compiler</artifactId>
                                    <version>1.8.0.1</version>
                                    <type>tar.gz</type>
                                    <classifier>distribution-linux</classifier>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${basedir}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.4.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <phase>generate-sources</phase>
                    </execution>
                </executions>
                <configuration>
                    <executable>${bin.path}</executable>
                    <workingDirectory>${fbs.sources}</workingDirectory>
                    <arguments>
                        <argument>--java</argument>
                        <argument>--gen-mutable</argument>
                        <argument>-o</argument>
                        <argument>${fbs.generated.sources}</argument>
                        <argument>${fbs.packet.directory}</argument>
                    </arguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.10</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${fbs.generated.sources}</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- end of flatbuffer-->

            <!-- for make jar executable -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>executable</classifier>
                            <mainClass>
                                com.gompang.ServerStarter
                            </mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

전체 pom.xml은 아래에서 참고

https://github.com/Gompangs/GNetServer/blob/master/pom.xml


위 pom.xml파일을 작성하고 빌드를 실행해보면 다음과 같다

(mvn clean package)



"C:\Program Files\Java\jdk1.8.0_101\bin\java" -Dmaven.multiModuleProjectDirectory=C:\Users\stack\Documents\JAVA_WORKSPACE\GNetServer "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\plugins\maven\lib\maven3\bin\m2.conf" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\lib\idea_rt.jar=14966:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2018.1 clean package spring-boot:repackage
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building GNetServer 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ GNetServer ---
[INFO] Deleting C:\Users\stack\Documents\JAVA_WORKSPACE\GNetServer\target
[INFO] 
[INFO] --- maven-dependency-plugin:2.10:unpack (unpack) @ GNetServer ---
[INFO] Configured Artifact: com.github.davidmoten:flatbuffers-compiler:distribution-linux:1.8.0.1:tar.gz
[INFO] Unpacking C:\Users\stack\.m2\repository\com\github\davidmoten\flatbuffers-compiler\1.8.0.1\flatbuffers-compiler-1.8.0.1-distribution-linux.tar.gz to C:\Users\stack\Documents\JAVA_WORKSPACE\GNetServer with includes "" and excludes ""
[INFO] Expanding: C:\Users\stack\.m2\repository\com\github\davidmoten\flatbuffers-compiler\1.8.0.1\flatbuffers-compiler-1.8.0.1-distribution-linux.tar.gz into C:\Users\stack\Documents\JAVA_WORKSPACE\GNetServer
[INFO] 
[INFO] --- exec-maven-plugin:1.4.0:exec (default) @ GNetServer ---
[INFO] 
[INFO] --- build-helper-maven-plugin:1.10:add-source (add-source) @ GNetServer ---
[INFO] Source directory: C:\Users\stack\Documents\JAVA_WORKSPACE\GNetServer\src\main\java added.
[INFO] 
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ GNetServer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ GNetServer ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 25 source files to C:\Users\stack\Documents\JAVA_WORKSPACE\GNetServer\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ GNetServer ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\stack\Documents\JAVA_WORKSPACE\GNetServer\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ GNetServer ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\Users\stack\Documents\JAVA_WORKSPACE\GNetServer\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.20.1:test (default-test) @ GNetServer ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestClient
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 s - in TestClient
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ GNetServer ---
[INFO] Building jar: C:\Users\stack\Documents\JAVA_WORKSPACE\GNetServer\target\GNetServer-1.0.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.0.0.RELEASE:repackage (default) @ GNetServer ---
[INFO] Attaching archive: C:\Users\stack\Documents\JAVA_WORKSPACE\GNetServer\target\GNetServer-1.0-executable.jar, with classifier: executable
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.0.0.RELEASE:repackage (default-cli) @ GNetServer ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.921 s
[INFO] Finished at: 2018-04-15T20:53:50+09:00
[INFO] Final Memory: 34M/234M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0




프로젝트 src/main/java/com/gompang/packet 에 packet.fbs에 기재된 클래스들이 자동 생성

/bin 디렉토리에 flatc executable 파일이 자동 생성



target classes의 역시 동일한 com/gompang/packet에 해당 패킷클래스들이 생성됨


packet.fbs는 간단하게 다음과 같다. (내용이 없음;)



// Example IDL file for our monster's schema.
namespace com.gompang.packet;

enum PacketType:byte {
    HeartBeat = 0,
    Login,
    Logout,
}

table HeartBeat {

}

table Login {
    id:string;
}

table Logout {
    id:string;
}


반응형

'Development > Netty & FlatBuffers' 카테고리의 다른 글

Netty 통신 서버 개발 관련 주저리  (1) 2018.04.05
[Flatbuffers] 플랫버퍼란?  (36) 2016.11.18