hacking2009/09/08 01:40
크리에이티브 커먼즈 라이선스
Creative Commons License
docbkx-tools는 편리한 대신, xslt와 fop의 세밀한 옵션을 조절할 수 없다. 그래서 좀 더 삽질을 해봤다.

말이 maven이지 ant로 했다면 훨씬 간단했을 듯...하지만, maven에서 xslt나 fop를 다루는 예제라고 생각하면 뭐... 손해볼 건 없다.

1. maven-dependency-pluginunpack-dependencies 골을 실행하여 docbook 스타일 시트와 카탈로그를 작업 디렉토리에 풀어 놓자. 예제에서는 generated-resources 페이즈에 net.sf.docbook:docbook-xsl 아티팩트를 target/generated-resources 폴더 아래에 풀어 놓는다(말이 참 어려운데... 가만히 들여다 보면 별거 아니다):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>unpack-shared-resources</id>
        <goals>
          <goal>unpack-dependencies</goal>
        </goals>
        <phase>generate-resources</phase>
        <configuration>
          <outputDirectory>${project.build.directory}/generated-resources</outputDirectory>
          <includeGroupIds>net.sf.docbook</includeGroupIds>
          <includeArtifactIds>docbook-xsl</includeArtifactIds>
          <includeClassifiers>resources</includeClassifiers>
          <excludeTransitive>true</excludeTransitive>
        </configuration>
      </execution>
    </executions>
</plugin>

2. xml-maven-plugintransform 골을 실행하여, 앞에서 풀어놓은 docbook 스타일 시트와 카탈로그들을 사용하여 xslt(여기서는 xalan을 사용했지만 saxon등 다른 xslt를 사용해도 된다)를 실행하여 fo 파일을 만든다.  예제에서는 generated-resources 페이즈에 src/docbook 디렉토리 아래에 manual.xml을 처리하여 처리하여 manual.fo인 파일을 만든다(parameters 요소를 사용하여 XSLT 파라메터를 지정할 수 있다):

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>xml-maven-plugin</artifactId>
   <version>1.0-beta-2</version>
   <executions>
    <execution>
      <goals>
        <goal>transform</goal>
      </goals>
      <!-- linked with generate-resources phase -->
      <phase>generate-resources</phase>
    </execution>
  </executions>
  <configuration>
      <catalogs>
        <catalog>${project.build.directory}/generated-resources/docbook/catalog.xml</catalog>
      </catalogs>
      <transformationSets>
        <transformationSet>
          <dir>src/docbook/</dir>
          <includes>
            <include>manual.xml</include>
          </includes>
          <stylesheet>${project.build.directory}/generated-resources/docbook/fo/docbook.xsl</stylesheet>
          <fileMappers>
            <fileMapper
 implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
            <targetExtension>.fo</targetExtension>
          </fileMapper>
        </fileMappers>
        <outputDir>${project.build.directory}/</outputDir>
        <!-- see http://docbook.sourceforge.net/release/xsl/current/doc/fo/ -->
        <parameters>
          <parameter>
            <name>body.font.family</name>
            <value>UnBatang</value>
          </parameter>
          <parameter>
            <name>sans.font.family</name>
            <value>UnDotum</value>
          </parameter>
          <parameter>
            <name>title.font.family</name>
            <value>hline</value>
          </parameter>
          <parameter>
            <name>monospace.font.family</name>
            <value>UnTaza</value>
          </parameter>
        </parameters>
      </transformationSet>
    </transformationSets>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>net.sf.docbook</groupId>
      <artifactId>docbook-xsl-xalan</artifactId>
      <version>1.0.0</version>
    </dependency>
  </dependencies>
</plugin>


3. maven-antrun-plugin의 run 골을 실행하여, fop를 실행하여 앞에서 만들어진 fo 파일들을 처리하여 pdf를 만들어 낸다. 이 부분은 maven에서 ant 스크립트를 사용하는 전형적인 형태다. 예제에서는 pre-site 페이즈에 fop에 포함된 ant task를 사용하여 manual.fo를 처리하여 manual.pdf를 만든다(userconfig 속성를 통해 fop 설정 파일을 지정할 수 있다):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.3</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.xmlgraphics</groupId>
      <artifactId>fop</artifactId>
      <version>0.95-1</version>
    </dependency>
  </dependencies>
  <executions>
    <execution>
      <phase>pre-site</phase>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  <configuration>
  <tasks>
    <echo message="processing docbook..." />
    <taskdef name="fop" classname="org.apache.fop.tools.anttasks.Fop">
    </taskdef>
    <fop
userconfig="src/docbook/fop-userconfig.xml"
format="application/pdf"
fofile="target/manual.fo"
outfile="target/manual.pdf" />
    </tasks>
  </configuration>
</plugin>

세삼 확인하게 되는 사실...
이런 류의 "절차적인" 삽질을 할때는 maven이 쥐약이라는 거...-,.-;;;

Posted by iolo
hacking2009/08/27 02:00
크리에이티브 커먼즈 라이선스
Creative Commons License
"maven and docbook"을 구글링하면 제일 먼저 나오는 녀석이 docbkx-tools인데, 그 덕분에 참고할 자료도 좀 있는 편이다.

pom.xml 파일에 다음의 내용을 추가해주고:

<plugin>
    <groupId>com.agilejava.docbkx</groupId>
    <artifactId>docbkx-maven-plugin</artifactId>
    <version>2.0.9</version>
    <executions>
        <execution>
            <goals>
                <goal>generate-pdf</goal>
                <goal>generate-html</goal>
            </goals>
             <!-- <phase>pre-site</phase> -->
        </execution>
    </executions>
    <dependencies>
    <dependency>
    <groupId>org.docbook</groupId>
    <artifactId>docbook-xml</artifactId>
    <version>4.4</version>
        </dependency>
    </dependencies>
    <configuration>
        <sourceDirectory>src/docbkx</sourceDirectory>
        <targetDirectory>target/docbkx</targetDirectory>
        <includes>**/*-manual.xml</includes>
        <entities>
            <entity>
                <name>version</name>
                <value>${pom.version}</value>
            </entity>
        </entities>
        <chunkedOutput>true</chunkedOutput>
        <highlightSource>1</highlightSource>
        <xincludeSupported>true</xincludeSupported>
        <!--  커스텀 스타일 시트 사용... -->
        <foCustomization>src/docbook/stylesheets/fo.xsl </foCustomization>
        <htmlCustomization>src/docbook/stylesheets/html.xsl </htmlCustomization>
        -->
        <!--  나눔 글꼴 설정 -->
        <bodyFontFamily>NanumMyeongjo</bodyFontFamily>
        <titleFontFamily>NanumGothic</titleFontFamily>
        <sansFontFamily>NanumGothic</sansFontFamily>
        <monospaceFontFamily>NanumGothic_Coding</monospaceFontFamily>
        <fonts>
            <font>
                <name>NanumMyeongjo</name>
                <style>normal</style>
                <weight>normal</weight>
                <embedFile>src/fonts/truetype/NanumMyeongjo.ttf </embedFile>
                <metricsFile>src/fonts/metrics/NanumMyeongjo-metrics.xml </metricsFile>
            </font>
            <font>
                <name>NanumMyeongjo</name>
                <style>normal</style>
                <weight>bold</weight>
                <embedFile>src/fonts/truetype/NanumMyeongjoBold.ttf </embedFile>
                <metricsFile>src/fonts/metrics/NanumMyeongjoBold-metrics.xml </metricsFile>
            </font>
            <font>
                <name>NanumGothic</name>
                <style>normal</style>
                <weight>normal</weight>
                <embedFile>src/fonts/truetype/NanumGothic.ttf </embedFile>
                <metricsFile>src/fonts/metrics/NanumGothic-metrics.xml </metricsFile>
            </font>
            <font>
                <name>NanumGothic</name>
                <style>normal</style>
                <weight>bold</weight>
                <embedFile>src/fonts/truetype/NanumGothicBold.ttf </embedFile>
                <metricsFile>src/fonts/metrics/NanumGothicBold-metrics.xml </metricsFile>
            </font>
            <font>
                <name>NanumGothic_Coding</name>
                <style>normal</style>
                <weight>normal</weight>
                <embedFile>src/fonts/truetype/NanumGothic_Coding.ttf </embedFile>
                <metricsFile>src/fonts/metrics/NanumGothic_Coding-metrics.xml </metricsFile>
            </font>
            <font>
                <name>NanumGothic_Coding</name>
                <style>normal</style>
                <weight>bold</weight>
                <embedFile>src/fonts/truetype/NanumGothic_Coding_Bold.ttf  </embedFile>
                <metricsFile>src/fonts/metrics/NanumGothic_Coding_Bold-metrics.xml </metricsFile>
            </font>
        </fonts>
    </configuration>
</plugin>

mvn docbkx:generate-pdf

명령을 실행하면 sourceDirectory(위의 예에서는 src/docbook) 아래에서 includes로 지정한 패턴(위의 예에서는 모든 하위디렉토리에서 -manual.xml로 끝나는 docbook 소스 파일을 처리해서 targetDirectory(위의 예에서는 target/docbook) 아래에 pdf로 만들어 준다.

덤으로 fop를 위한 폰트 매트릭을 만들어 주는 플러그인도 있다.

pom.xml 파일에 다음의 내용을 추가해주고:

<plugin>
    <groupId>com.agilejava.docbkx</groupId>
    <artifactId>docbkx-fop-support</artifactId>
    <!--
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    -->
    <configuration>
        <!-- <ansi>true</ansi> -->
        <sourceDirectory>src/fonts/truetype</sourceDirectory>
        <targetDirectory>src/fonts/metrics</targetDirectory>
    </configuration>
</plugin>

mvn docbkx-fop-support:generate

위의 명령을 실행하면 sourceDirectory(위의 예에서는 src/fonts/truetype)안에 있는 *.ttf 파일들을 처리해서  targetDirectory(위의 예에서는 src/fonts/metrics)에 파일 이름 끝에 -metrics.xml을 붙인 메트릭 파일을 생성한다.

위에서는 코멘트 쳐둔 excutions절을 사용하면 빌드 과정에 포함시킬 수 있지만, 시간도 꽤 오래걸리는 작업이고, 큰 글꼴의 경우엔 out of memory도 잘 나고, 결정적으로 대부분의 한글 글꼴들의 경우엔 폰트 헤더의 인코딩 때문에 문제가 있어서 수작업으로 처리했다.(그래서 targetDirectory가 target/...이 아니고 src/...다)

덧. 수작업이라는 건 만들어진 메트릭 파일에서 <family-name>...</family-name>사이에 들어 있는 요상한 문자들을 좋은 편집기(절때 notepad나 eclipse같은 멍청한 편집기 쓰면 안된다!)로 열어서 알아보기 쉬운 영어로 고쳐주던가... <family-name> 태그 자체를 날려버리면 된다.

Posted by iolo