Maven - 在主模块中运行子模块的插件任务

时间:2016-07-07 17:45:33

标签: java maven module

我有一个maven项目(java web backend)设置了一个子模块,该子模块使用JSweet将我的java源代码转换为javascript文件(前端)。

我的目标是将所有子模块的源转换到文件夹“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服务器时转换源。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分: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 :(得分:1)

为了在webapp中使用您的子模块,您可以将前端项目/模块打包为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

Candy在依赖项中被引用并提取到 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中:)