2014-03-26 30 views
2

我正在使用多個文件中定義的多個本體的項目。我希望使用Jena生成Java類來幫助開發,但我似乎無法找到一種方法讓Jena處理多個文件作爲Maven目標。在處理多個本體的maven中的Jena schemagen

我從來沒有通過maven使用Jena,但我已經在命令行上使用過它(從來沒有在多個本體上同時使用過)。

我的pom.xml的相關部分列舉如下(這在很大程度上是從耶拿網站複製):

<plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>exec-maven-plugin</artifactId> 
      <executions> 
       <execution> 
        <phase>generate-sources</phase> 
        <goals> 
         <goal>java</goal> 
        </goals> 
        <configuration> 
         <mainClass>jena.schemagen</mainClass> 
         <commandlineArgs> 
          --inference \ 
          -i ${basedir}/src/main/resources/ontologies/exception.owl \ 
          --package com.borwell.dstl.mexs.ontology \ 
          -o ${project.build.directory}/generated-sources/java \ 
         </commandlineArgs> 
         <commandlineArgs> 
          --inference \ 
          -i ${basedir}/src/main/resources/ontologies/location.owl \ 
          --package com.borwell.dstl.mexs.ontology \ 
          -o ${project.build.directory}/generated-sources/java \ 
         </commandlineArgs> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
</plugins> 

我有耶拿網站和其他網站上做一件事(我googlefoo通常相當不錯),但我一直無法找到任何其他人有這個問題或任何文件解釋如何做到這一點。 對此的任何幫助將是非常有用的。

+1

以前從未使用Jena,但對於maven,您可以簡單地定義2次執行maven-exec-plugin並讓jena以這種方式執行兩次。這不能解決你的問題嗎? – DB5

回答

0

經過更全面的搜索和感謝DB5我發現解決方案是多次執行。我希望有一個更優雅的解決方案,但這確實奏效。

感謝DB5

+0

添加解決方案的一些描述或鏈接到相關答案將會很有用。就目前而言,答案只是鏈接到用戶的個人資料,沒有任何指導。 –

+0

我指的是DB5對我的原始問題的評論,它提供了明顯的解決方案。 – egmackenzie

2

編輯

This Answer現在存在,並調用了較爲標準的解決方案。也就是說,海報開發的schemagen-maven plugin存在於標準的耶拿分佈中。

原來的答案

你有兩個選擇,創建一個自定義Maven插件(這其實並不難),或掏出來的東西以外Maven來處理您的需求。我過去做了一個黑客攻擊,我將與你分享。這並不漂亮,但效果很好。

該方法使用Maven Ant Tasks從maven構建的generate-sources階段中調用到ant build.xml

你開始通過修改pom.xml到包括以下內容:

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-antrun-plugin</artifactId> 
      <version>1.7</version> 
      <executions> 
       <execution> 
        <phase>generate-sources</phase> 
        <goals> 
         <goal>run</goal> 
        </goals> 
        <configuration> 
         <target> 
          <taskdef resource="net/sf/antcontrib/antcontrib.properties" 
           classpathref="maven.runtime.classpath" /> 
          <property name="runtime-classpath" refid="maven.runtime.classpath" /> 
          <ant antfile="${basedir}/src/main/ant/build.xml" 
           inheritRefs="true"> 
           <target name="my.generate-sources" /> 
          </ant> 
         </target> 
        </configuration> 
       </execution> 
      </executions> 
      <dependencies> 
       <dependency> 
        <groupId>ant-contrib</groupId> 
        <artifactId>ant-contrib</artifactId> 
        <version>1.0b3</version> 
        <exclusions> 
         <exclusion> 
          <groupId>ant</groupId> 
          <artifactId>ant</artifactId> 
         </exclusion> 
        </exclusions> 
       </dependency> 
      </dependencies> 
     </plugin> 
    </plugins> 
</build> 

build.xml文件(其中,你可以看到,位於src/main/ant/build.xml,就是下面

<?xml version="1.0" encoding = "UTF-8"?> 
<!DOCTYPE project> 
<project name="my" default="my.error"> 

    <property name="vocab.out.root" location="${basedir}/src/main/java" /> 
    <property name="vocab.package" value = "put something here" /> 
    <property name="vocab.ns" value="put something here" /> 

    <path id="my.vocabulary"> 
     <fileset dir="${basedir}/src/main/resources/put something here" casesensitive="yes" > 
      <include name="**/*.owl" /> 
     </fileset> 
    </path> 

    <scriptdef language="javascript" name="make-proper"> 
     <attribute name="string" /> 
     <attribute name="to" /> 
     <![CDATA[ 
      var raw_string = attributes.get("string"); 
      var string_elements = raw_string.split('-'); 
      var nameComponents; 
      var finalName = ""; 

      var i; 
      for(i = 0; i < string_elements.length; i++){ 
       var element = string_elements[i]; 
       finalName += element.substr(0,1).toUpperCase() + element.substr(1); 
      } 

      project.setProperty(attributes.get("to"), finalName); 
     ]]> 
    </scriptdef> 

    <!-- target for processing a file --> 
    <target name="my.schemagen-file"> 

     <echo message="${vocab.file}" /> 

     <!-- for constructing vocab file name --> 
     <local name="source.file.dir" /> 
     <dirname property="source.file.dir" file="${vocab.file}" /> 
     <local name="source.file" /> 
     <basename property="source.file" file="${vocab.file}" suffix=".owl" /> 
     <local name="vocab.name" /> 
     <make-proper string="${source.file}" to="vocab.name" /> 

     <!-- for constructing destination file name --> 
     <local name="vocab.package.path" /> 
     <propertyregex property="vocab.package.path" input="${vocab.package}" regexp="\." replace="/" global="true" /> 
     <local name="dest.file" /> 
     <property name="dest.file" value="${vocab.out.root}/${vocab.package.path}/${vocab.name}.java" /> 

     <!-- Determine if we should build, then build -->  
     <outofdate> 
      <sourcefiles path="${vocab.file}" /> 
      <targetfiles path="${dest.file}" /> 
      <sequential> 
       <!-- Actual construction of the destination file --> 
       <echo message="--inference --ontology -i ${vocab.file} -a ${vocab.ns} --package ${vocab.package} -o ${vocab.out.root} -n ${vocab.name}" /> 
       <java classname="jena.schemagen" classpath="${runtime-classpath}"> 
        <arg line="--ontology --nostrict -i ${vocab.file} -a ${vocab.ns} --package ${vocab.package} -o ${vocab.out.root} -n ${vocab.name}" /> 
       </java> 
      </sequential> 
     </outofdate> 
    </target> 

    <!-- Maven antrun target to generate sources --> 
    <target name="my.generate-sources"> 
     <foreach target="my.schemagen-file" param="vocab.file" inheritall="true"> 
      <path refid="my.vocabulary"/> 
     </foreach> 
    </target> 

    <target name="my.error"> 
     <fail message="This is not intended to be executed from the command line. Execute generate-sources goal using maven; ex:\nmvn generate-sources" /> 
    </target> 
</project> 

走過你的整個事情。

  1. maven-antrun-plugin執行我們的構建腳本,確保antcontrib可用以及maven.runtime.classpath。這確保了maven中可用的屬性也可以在ant任務中使用。
  2. 如果調用build.xml而未指定任務,則故意失敗。這有助於防止您錯誤地使用它。
  3. 如果執行my.generate-sources目標,那麼我們可以使用antcontrib中的foreach來處理指定目錄中的每個文件。無論您需要如何調整此文件模式。請注意,其他地方使用了假設的.owl的擴展名。
  4. 對於每個owl文件,調用my.schemagen-file目標。我利用local variables爲了使這個功能。
  5. 自定義的ant任務(在javascript中定義,名爲make-proper)有助於爲您的文件生成目標文件名。我使用一個約定,我的詞彙檔案在some-hyphenated-lowercase-structure.owl。我期望的班級名稱是SomeHyphenatedLowercaseStructure.java。不管你認爲合適,你應該自定義這個。
  6. 對於每個派生的文件名,我們測試它是否存在以及它是否過期。如果源文件比目標文件新,這種便利性只允許運行schemagen。我們使用outofdate task來做到這一點。
  7. 我們運行schemagen。我使用特定於OWL詞彙表的標誌。你可以調整它,但最適合你的系統。
+0

不錯的建議!但是我錯過了調用同一事件的多次執行的能力 - 由DB5指出。儘管我會將你的建議銘記於未來!謝謝 – egmackenzie

3

我爲Jena Schemagen寫了一個自定義的Maven任務,它現在是標準Jena發行版的一部分。請參閱the documentation here