2016-07-07 64 views
2

我有一个使用JSweet将我的java源代码转换为javascript文件(前端)的子模块设置的maven项目(java web后端)。Maven在主模块中运行子模块的插件任务

我的目标是将所有子模块的源文件转译成文件夹“src/main/java/views/script”,其中主模块从中加载JavaScript文件。

这是子模块的POM样子对于这个问题:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 

<groupId>com.domain</groupId> 
<artifactId>domain-frontend</artifactId> 
<version>1.0-SNAPSHOT</version> 

<!-- jsweet --> 
<pluginRepositories> 
    <pluginRepository> 
     <id>jsweet-plugins-release</id> 
     <name>plugins-release</name> 
     <url>http://repository.jsweet.org/artifactory/plugins-release-local</url> 
    </pluginRepository> 
    <pluginRepository> 
     <snapshots /> 
     <id>jsweet-plugins-snapshots</id> 
     <name>plugins-snapshot</name> 
     <url>http://repository.jsweet.org/artifactory/plugins-snapshot-local</url> 
    </pluginRepository> 
</pluginRepositories> 


<build> 
    <plugins> 
     <plugin> 
      <groupId>org.jsweet</groupId> 
      <artifactId>jsweet-maven-plugin</artifactId> 
      <version>1.0.0-SNAPSHOT</version> 
      <configuration> 
       <outDir>src/main/java/views/script</outDir> 
       <targetVersion>ES3</targetVersion> 
       <verbose>true</verbose> 
       <encoding>UTF-8</encoding> 
      </configuration> 
      <executions> 
       <execution> 
        <id>generate-js</id> 
        <phase>generate-sources</phase> 
        <goals> 
         <goal>jsweet</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

</project> 

我已经添加了这个子模块主要项目的依赖关系:

<dependency> 
     <groupId>com.domain</groupId> 
     <artifactId>domain-frontend</artifactId> 
     <version>1.0-SNAPSHOT</version> 
    </dependency> 

现在我想前端的JSweet插件任务作为主模块中的插件运行,以便在重新加载web服务器时传输源代码。我怎样才能做到这一点?

回答

1

我得到这个做以下工作:

1)创建一个新的Maven项目
2)添加的后端和前端作为模块,像这样:

<modules> 
    <module>frontend</module> 
    <module>backend</module> 
</modules> 

在前置式和后端注册父项目:

<parent> 
    <artifactId>domain-webapp</artifactId> 
    <groupId>com.domain.webapp</groupId> 
    <version>1.0-SNAPSHOT</version> 
</parent> 

现在,每当我做

mvn package ninja:run -pl backend 

它首先调用将Java源代码编译为Javascript文件的前端插件,然后编译后端。

ninja:run任务(来自我的框架)在文件被更改时自动重建,这就是为什么对我来说重要的是JSweet文件也被重新编译。

由于ninja:run只为后端模块所知,所以我不得不使用-pl backend

希望我能帮助别人!

1

为了在web应用程序使用你的子模块,你可以打包前端项目/模块作为JSweet糖果,以引用它作为你的后端项目的糖果依赖。这个主题在这里部分解释: https://github.com/cincheo/jsweet/blob/v1.1.1/doc/jsweet-language-specifications.md#packaging-a-jsweet-jar-candy

我给你下面的Maven配置对我有用。

前端项目POM

请注意OUTDIR src/main/resources/META-INF/resources/webjars

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>fr.xxxx</groupId> 
    <artifactId>frontend</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <repositories> 
     <repository> 
      <id>jsweet-central</id> 
      <name>libs-release</name> 
      <url>http://repository.jsweet.org/artifactory/libs-release-local</url> 
     </repository> 
     <repository> 
      <id>jsweet-external</id> 
      <name>libs-release</name> 
      <url>http://repository.jsweet.org/artifactory/ext-release-local</url> 
     </repository> 
     <repository> 
      <snapshots /> 
      <id>jsweet-snapshots</id> 
      <name>libs-snapshot</name> 
      <url>http://repository.jsweet.org/artifactory/libs-snapshot-local</url> 
     </repository> 
    </repositories> 
    <pluginRepositories> 
     <pluginRepository> 
      <id>jsweet-plugins-release</id> 
      <name>plugins-release</name> 
      <url>http://repository.jsweet.org/artifactory/plugins-release-local</url> 
     </pluginRepository> 
     <pluginRepository> 
      <snapshots /> 
      <id>jsweet-plugins-snapshots</id> 
      <name>plugins-snapshot</name> 
      <url>http://repository.jsweet.org/artifactory/plugins-snapshot-local</url> 
     </pluginRepository> 
    </pluginRepositories> 

    <build> 

     <sourceDirectory>src/main/java</sourceDirectory> 
     <resources> 
      <resource> 
       <directory>src/main/resources</directory> 
       <filtering>true</filtering> 
      </resource> 
     </resources> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.3</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
        <fork>true</fork> 
       </configuration> 
      </plugin> 

      <plugin> 
       <groupId>org.jsweet</groupId> 
       <artifactId>jsweet-maven-plugin</artifactId> 
       <version>1.2.0-SNAPSHOT</version> 
       <configuration> 
        <bundle>true</bundle> 
        <outDir>src/main/resources/META-INF/resources/webjars/${project.name}/${project.version}</outDir> 
        <tsOut>.jsweet/ts</tsOut> 
        <targetVersion>ES5</targetVersion> 
        <verbose>true</verbose> 
        <declaration>true</declaration> 
        <dtsOut>src/main/resources/src/typings</dtsOut> 
        <encoding>UTF-8</encoding> 
       </configuration> 
       <executions> 
        <execution> 
         <id>generate-js</id> 
         <phase>generate-sources</phase> 
         <goals> 
          <goal>jsweet</goal> 
         </goals> 
        </execution> 
        <execution> 
         <id>clean</id> 
         <phase>clean</phase> 
         <goals> 
          <goal>clean</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <artifactId>maven-clean-plugin</artifactId> 
       <version>3.0.0</version> 
       <configuration> 
        <filesets> 
         <fileset> 
          <directory>src/main/resources/src/typings</directory> 
          <includes> 
           <include>**/*</include> 
          </includes> 
         </fileset> 
         <fileset> 
          <directory>src/main/resources/META-INF/resources/webjars</directory> 
          <includes> 
           <include>**/*</include> 
          </includes> 
         </fileset> 
        </filesets> 
       </configuration> 
      </plugin> 
     </plugins> 

后端项目的pom

糖果在依赖参考,并提取到 src/main/webapp/js/candies 它仍然是手动,你必须建立你的糖果,那么你的后端使用 mvn clean generate-sources -P client 您可以删除个人资料,我认为这是无用的重新部署您

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>fr.xxxx</groupId> 
    <artifactId>backend</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>Backend</name> 

    <repositories> 
     <repository> 
      <id>jsweet-central</id> 
      <name>libs-release</name> 
      <url>http://repository.jsweet.org/artifactory/libs-release-local</url> 
     </repository> 
     <repository> 
      <id>jsweet-external</id> 
      <name>libs-release</name> 
      <url>http://repository.jsweet.org/artifactory/ext-release-local</url> 
     </repository> 
     <repository> 
      <snapshots /> 
      <id>jsweet-snapshots</id> 
      <name>libs-snapshot</name> 
      <url>http://repository.jsweet.org/artifactory/libs-snapshot-local</url> 
     </repository> 
    </repositories> 

    <properties> 
     <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <jersey.version>2.22.2</jersey.version> 
     <src.dir>src/main/java</src.dir> 
    </properties> 

    <profiles> 
     <profile> 
      <id>client</id> 
      <properties> 
       <src.dir>src/main/jsweet</src.dir> 
      </properties> 

      <build> 
       <plugins> 
        <plugin> 
         <groupId>org.jsweet</groupId> 
         <artifactId>jsweet-maven-plugin</artifactId> 
         <version>1.2.0-SNAPSHOT</version> 
         <configuration> 
          <bundle>false</bundle> 
          <module>none</module> 
          <outDir>src/main/webapp/js/app</outDir> 
          <tsOut>.jsweet/ts</tsOut> 
          <candiesJsOut>src/main/webapp/js/candies</candiesJsOut> 
          <targetVersion>ES5</targetVersion> 
          <verbose>true</verbose> 
          <includes> 
           <include>**/client/**/*.java</include> 
          </includes> 
         </configuration> 
         <executions> 
          <execution> 
           <id>generate-js</id> 
           <phase>generate-sources</phase> 
           <goals> 
            <goal>jsweet</goal> 
           </goals> 
          </execution> 
          <execution> 
           <id>clean</id> 
           <phase>clean</phase> 
           <goals> 
            <goal>clean</goal> 
           </goals> 
          </execution> 
         </executions> 
        </plugin> 

        <plugin> 
         <artifactId>maven-clean-plugin</artifactId> 
         <version>3.0.0</version> 
         <configuration> 
          <filesets> 
           <fileset> 
            <directory>src/main/webapp/js/candies</directory> 
            <includes> 
             <include>**/*</include> 
            </includes> 
           </fileset> 
          </filesets> 
         </configuration> 
        </plugin> 
       </plugins> 
      </build> 
     </profile> 
    </profiles> 

    <dependencies> 
[...] 
     <dependency> 
      <groupId>fr.xxxx</groupId> 
      <artifactId>frontend</artifactId> 
      <version>0.0.1-SNAPSHOT</version> 
      <scope>compile</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.jsweet</groupId> 
      <artifactId>jsweet-transpiler</artifactId> 
      <version>1.2.0-SNAPSHOT</version> 
     </dependency> 
     <dependency> 
      <groupId>org.jsweet.candies</groupId> 
      <artifactId>jsweet-core</artifactId> 
      <version>1.2.0-SNAPSHOT</version> 
     </dependency> 
    </dependencies> 

    <build> 
     <finalName>backend</finalName> 
     <sourceDirectory>${src.dir}</sourceDirectory> 
     <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>2.1.1</version> 
      <configuration> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
      <packagingExcludes>WEB-INF/web.xml</packagingExcludes> 
      <packagingExcludes>**/fr/xxx/client/*</packagingExcludes> 
      <warName>backend</warName> 
      </configuration> 
     </plugin> 
     </plugins> 
    </build> 
</project> 

NB :就你而言,将前端直接放在你的web项目中可能会更好,注意在我们的pom中,有2个源代码目录:src/main/java和src/main/jsweet,它可能是一个解决方案您。这对我来说很有意义,JSweet的前端来源类似于JavaScript资源,它们驻留在webapp中:)