asciidoctor / asciidoctor-gradle-plugin

A Gradle plugin that uses Asciidoctor via JRuby to process AsciiDoc source files within the project.
https://asciidoctor.github.io/asciidoctor-gradle-plugin/
Apache License 2.0
285 stars 120 forks source link

HELP - How to add dependencies to asciidoctorPdf - asciidoctorj #647

Closed life888888 closed 1 year ago

life888888 commented 1 year ago

I am package some ttf and theme to a jar file (pdf-ext-notoserif-cjk-tc.jar) like this

└── data
    ├── fonts
    │   ├── notosans-cjk-tc-bold_italic.ttf
    │   ├── notosans-cjk-tc-bold.ttf
    │   ├── notosans-cjk-tc-italic.ttf
    │   └── notosans-cjk-tc-normal.ttf
    └── themes
        └── default-ext-notosans-cjk-tc-theme.yml

I install this jar to my local maven

mvn install:install-file \
  -Dfile=pdf-ext-notoserif-cjk-tc.jar \
  -DgroupId=com.life888888.lab \
  -DartifactId=pdf-ext-notoserif-cjk-tc \
  -Dversion=1.0.0 \
  -Dpackaging=jar

I can add this jar in maven pom.xml (asciidoctor-pdf-with-theme-example)

pom.xml.txt

    <build>
        <defaultGoal>process-resources</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>${asciidoctor.maven.plugin.version}</version>
                <dependencies>
                    <dependency>
                        <groupId>org.asciidoctor</groupId>
                        <artifactId>asciidoctorj-pdf</artifactId>
                        <version>${asciidoctorj.pdf.version}</version>
                    </dependency>
                    <!-- Comment this section to use the default jruby artifact provided by the plugin -->
                    <dependency>
                        <groupId>org.jruby</groupId>
                        <artifactId>jruby</artifactId>
                        <version>${jruby.version}</version>
                    </dependency>
                    <!-- Comment this section to use the default AsciidoctorJ artifact provided by the plugin -->
                    <dependency>
                        <groupId>org.asciidoctor</groupId>
                        <artifactId>asciidoctorj</artifactId>
                        <version>${asciidoctorj.version}</version>
                    </dependency>
                        <dependency>
                           <groupId>com.life888888.lab</groupId>
                           <artifactId>pdf-ext-notoserif-cjk-tc</artifactId>
                           <version>1.0.0</version>
                       </dependency>
...
                        <configuration>
                            <backend>pdf</backend>
                            <outputDirectory>${project.build.directory}/generated-docs-custom-theme</outputDirectory>
                            <!-- Use `book` docType to enable title page generation -->
                            <doctype>book</doctype>
                            <attributes>
                                <pdf-fontsdir>uri:classloader:/data/fonts;GEM_FONTS_DIR;</pdf-fontsdir>        
                                <pdf-themesdir>uri:classloader:/data/themes</pdf-themesdir>
                                <pdf-theme>default-ext-notosans-cjk-tc</pdf-theme>
...

I can successful , generate a PDF with CJK fonts and theme.

But , I can not add my pdf-ext-notoserif-cjk-tc.jar to gradle dependencies

https://github.com/asciidoctor/asciidoctor-gradle-examples/tree/master/asciidoc-to-pdf-example/build.gradle

build.gradle.txt

repositories {
    jcenter()
    ruby.gems()
    mavenLocal()
}

dependencies {
    runtimeOnly 'com.life888888.lab:pdf-ext-notosansmono-cjk-tc:1.0.0'
    implementation files('/home/demo/asciidoctor-pdf-cjk-ext/releases/pdf-ext-notosansmono-cjk-tc.jar') 
    compile 'com.life888888.lab:pdf-ext-notosansmono-cjk-tc:1.0.0'
    runtime 'com.life888888.lab:pdf-ext-notosansmono-cjk-tc:1.0.0'
    implementation ('com.life888888.lab:pdf-ext-notosansmono-cjk-tc:1.0.0')

    asciidoctorGems 'rubygems:rouge:3.15.0'
}
...
asciidoctorPdf {
  dependsOn asciidoctorGemsPrepare
  baseDirFollowsSourceFile()
  asciidoctorj {
    requires 'rouge'
    attributes 'build-gradle': file('build.gradle'),
        'scripts': 'cjk',
        'pdf-fontsdir': 'uri:classloader:/data/fonts;GEM_FONTS_DIR;',
        'pdf-themesdir': 'uri:classloader:/data/themes',
        'pdf-theme': 'default-ext-notosans-cjk-tc',
        'sourcedir': project.sourceSets.main.java.srcDirs[0],
        'endpoint-url': 'http://example.org',
        'source-highlighter': 'rouge',
        'imagesdir': './images',
        'toc': 'left',
        'icons': 'font',
        'idprefix': '',
        'idseparator': '-'
  }
}
...

Then I run

./gradlew --info asciidoctor

I can find something , but I do not see my dependencies jar in AsciidoctorJ instance classpath.

Running AsciidoctorJ instance with classpath [/home/demo/.gradle/caches/modules-2/files-2.1/org.asciidoctor/asciidoctor-gradle-jvm/3.3.0/4f65c...

SO, HOW TO ADD dependencies jar TO Gradle Task asciidoctorPdf with AsciidoctorJ instance classpath ???

mrotteveel commented 1 year ago

I haven't tried it myself, but I guess you're looking for the custom extensions option described at https://asciidoctor.github.io/asciidoctor-gradle-plugin/development-3.x/user-guide/#asciidoctorj-extensions

life888888 commented 1 year ago

YES , THANK YOU.

I am package my CJK TTF and theme (base on asciidoctor-pdf-2.x)

pdf-ext-notosans-cjk-tc.jar

.
├── data
│   ├── fonts
│   │   ├── LICENSE
│   │   ├── notosans-cjk-tc-bold_italic.ttf
│   │   ├── notosans-cjk-tc-bold.ttf
│   │   ├── notosans-cjk-tc-italic.ttf
│   │   └── notosans-cjk-tc-normal.ttf
│   └── themes
│       ├── default-ext-notosans-cjk-tc-pdf-1-theme.yml
│       ├── default-ext-notosans-cjk-tc-theme.yml
│       ├── default-notosans-cjk-tc-pdf-1-theme.yml
│       ├── default-notosans-cjk-tc-theme.yml
│       ├── LICENSE
│       ├── LICENSE-asciidoctor-pdf
│       ├── LICENSE-asciidoctor-pdf-cjk-kai_gen_gothic
│       ├── notosans-cjk-tc-pdf-1-theme.yml
│       └── notosans-cjk-tc-theme.yml
└── META-INF
    └── MANIFEST.MF

And I installed my jar to my local Maven Repo (~/.m2)

mvn install:install-file \
  -Dfile=pdf-ext-notosans-cjk-tc.jar \
  -DgroupId=com.life888888.lab \
  -DartifactId=pdf-ext-notosans-cjk-tc \
  -Dversion=1.0.0 \
  -Dpackaging=jar

Then I download asciidoctor-gradle-examples

asciidoctor-gradle-examples-master/asciidoc-to-pdf-example

and I change build.gradle

plugins {
  id 'org.asciidoctor.jvm.pdf' version '3.3.0'
  id 'org.asciidoctor.jvm.gems' version '3.3.0'
}

apply plugin: 'java'

version = '1.0.0-SNAPSHOT'

repositories {
    jcenter()
    ruby.gems()
   //ADD NEW
    mavenLocal()    
}

//ADD NEW
configurations {
    asciidoctorExt
}

dependencies {
  asciidoctorGems 'rubygems:rouge:3.15.0'
 //ADD NEW
  //asciidoctorExt 'com.life888888.lab:pdf-ext-notoserif-cjk-tc:1.0.0'
  asciidoctorExt 'com.life888888.lab:pdf-ext-notosans-cjk-tc:1.0.0'  
}

asciidoctorPdf {
  dependsOn asciidoctorGemsPrepare

  baseDirFollowsSourceFile()

  asciidoctorj {
   //ADD NEW
     modules {
       asciidoctorj {
         version '2.5.6'
         configurations 'asciidoctorExt'
       }
       pdf {
         version '2.3.0'
       }
     }

    requires 'rouge'
    attributes 'build-gradle': file('build.gradle'),    
        'sourcedir': project.sourceSets.main.java.srcDirs[0],
        'endpoint-url': 'http://example.org',
        'source-highlighter': 'rouge',
        'imagesdir': './images',
        'toc': 'left',
        'icons': 'font',
        'idprefix': '',
        'idseparator': '-',
        'scripts': 'cjk',
        'pdf-fontsdir': 'uri:classloader:/data/fonts;GEM_FONTS_DIR;',
        'pdf-themesdir': 'uri:classloader:/data/themes',
        'pdf-theme': 'default-ext-notosans-cjk-tc'
  }
}

// alias
task asciidoctor(dependsOn: asciidoctorPdf)

Now I can use asciidoctor-pdf 2.x and apply my jar (CJK fonts and themes)

647 and #648

THANK YOU AGAIN.