如何使用Maven在Vaadin 7中仅编译必要的小部件?

时间:2014-01-08 19:55:41

标签: java eclipse maven vaadin vaadin7

我是Vaadin框架的新手,我看起来非常有趣,使用eclipse和maven来开发和构建我的应用程序我觉得很烦人,每次我做mvn clean安装时都需要很长时间来构建应用程序,我发现这是因为它编译了整套小部件。

即使我在布局中只使用了一个按钮,构建应用程序也会花费很多。

我已经在互联网和2本书中研究了一段时间,但是找不到足够的信息,关于如何使它只编译我正在使用的组件,而不是整套。

我使用maven原型创建了项目:

mvn archetype:generate -DarchetypeGroupId=com.vaadin -DarchetypeArtifactId=vaadin-archetype-application -DarchetypeVersion=7.1.9

我确信每次构建战争时都会编译widgetset,当我执行mvn clean时会删除目录:/ src / main / webapp / VAADIN / widgetsets和/ src / main / webapp / VAADIN / GWT-unitCache

当我运行mvn install时,构建将持续超过3分钟:

...
[INFO]    Compiling 6 permutations
[INFO]       Compiling permutation 0...
[INFO]       Process output
[INFO]          Compiling
[INFO]             Compiling permutation 1...
[INFO]       Process output
[INFO]          Compiling
[INFO]             Compiling permutation 3...
[INFO]       Process output
[INFO]          Compiling
[INFO]             Compiling permutation 2...
[INFO]       Compiling permutation 4...
[INFO]          Compiling
[INFO]             Compiling permutation 5...
[INFO]    Compile of permutations succeeded
[INFO] Linking into /.../example/src/main/webapp/VAADIN/widgetsets/com.my.example.AppWidgetSet; Writing extras to /.../example/target/extra/com.my.example.AppWidgetSet
[INFO]    Link succeeded
[INFO]    Compilation succeeded -- 167.103s
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ ade ---
[INFO] Packaging webapp
[INFO] Assembling webapp [example] in [/.../example/target/example-0.1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/.../example/src/main/webapp]
[INFO] Webapp assembled in [562 msecs]
[INFO] Building war: /.../example/target/example-0.1.0-SNAPSHOT.war
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ ade ---
[INFO] Installing /.../example/target/example-0.1.0-SNAPSHOT.war to /.../example/0.1.0-SNAPSHOT/example-0.1.0-SNAPSHOT.war
[INFO] Installing /.../example/pom.xml to /.../example/0.1.0-SNAPSHOT/example-0.1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3:03.768s
[INFO] Finished at: Fri Jan 10 00:10:45 EST 2014
[INFO] Final Memory: 16M/81M
[INFO] ------------------------------------------------------------------------

在此之后,再次生成包含以下目录的目录/ src / main / webapp / VAADIN / widgetsets:

WEB-INF             
com.my.example.AppWidgetSet

它还会生成/ src / main / webapp / VAADIN / gwt-unitCache

4 个答案:

答案 0 :(得分:22)

您需要自定义widgetset吗?如果您没有使用任何小部件插件,并且因为您是Vaadin的新用户,我假设您还没有创建自己的小部件(?),您可以简单地使用Vaadin提供的预编译小部件集。为此,请从项目中删除任何xxx.gwt.xml文件,并使用com.vaadin.DefaultWidgetset替换web.xml中对它的引用。

的web.xml:

<init-param>
    <name>widgetset</name>
    <value>com.vaadin.DefaultWidgetSet</value>
</init-param>

的pom.xml:

<dependency>
    <groupId>com.vaadin</groupId>
    <artifactId>vaadin-client-compiled</artifactId>
    <version>7.1.9</version>    <!-- or whatever version you're using -->
</dependency>

如果你确实需要一个自定义的widgetset(如果你现在没有,那么很可能你需要更进一步),帮自己一个忙,把它放在一个单独的项目中。根据我的经验,一个widgetset很少变化,所以为什么要将它包含在一个不断变化的项目中。由Vaadin提供的上述默认widgetset是构建一个的完美蓝图。只需构建自己的,并从vaadin-client-compiled.jar复制其结构。你可以使用你最喜欢的maven build helper,我的是程序集。只需创建一个maven项目,设置pom.xml,添加一个xxx.gwt.xml并确保web.xml包含对它的引用。我自己的设置看起来就像你在下面看到的那样。

的pom.xml:

<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>

    <name>MyWidgetset</name>
    <groupId>com.company</groupId>
    <artifactId>mywidgetset</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <vaadin.version>7.1.9</vaadin.version>
        <vaadin.plugin.version>7.1.9</vaadin.plugin.version>
    </properties>

    <dependencies>
        <!-- vaadin -->
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-client</artifactId>
            <version>${vaadin.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-client-compiler</artifactId>
            <version>${vaadin.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- custom widgets (NOTE: addons without a widget do not belong here) -->
        <dependency>
            <groupId>org.vaadin.addons</groupId>
            <artifactId>filteringtable</artifactId>
            <version>0.9.3.v7</version>
        </dependency>
        <dependency>
            <groupId>org.vaadin.addons</groupId>
            <artifactId>popupbutton</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- vaadin update widgetset -->
            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>${vaadin.plugin.version}</version>

                <configuration>
                    <extraJvmArgs>-Xmx512M -Xss1024k</extraJvmArgs>
                    <webappDirectory>${basedir}/target/VAADIN/widgetsets</webappDirectory>
                    <hostedWebapp>${basedir}/target/VAADIN/widgetsets</hostedWebapp>
                    <force>false</force>
                    <strict>true</strict>
                    <noServer>true</noServer>
                    <compileReport>true</compileReport>
                    <style>OBF</style>
                    <runTarget>http://localhost:8080/</runTarget>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>resources</goal>
                            <goal>update-widgetset</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/resources/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <repositories>
        <repository>
            <id>vaadin-addons</id>
            <url>http://maven.vaadin.com/vaadin-addons</url>
        </repository>
    </repositories>
</project>

assembly.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>build-my-widgetset-jar</id>
    <formats>
        <format>jar</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>${basedir}/target/VAADIN/widgetsets</directory>
            <outputDirectory>/VAADIN/widgetsets</outputDirectory>
            <excludes>
                <exclude>WEB-INF/</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

MyWidgetset.gwt.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
    "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN"
    "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
<module>
    <inherits name="com.vaadin.DefaultWidgetSet" />

    <inherits name="org.tepi.filtertable.gwt.FilterTableWidgetset" />

    <inherits name="org.vaadin.hene.popupbutton.widgetset.PopupbuttonWidgetset" />

</module>

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <display-name>MyWidgetset</display-name>

    <servlet>
        <servlet-name>MyWidgetset</servlet-name>
        <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
        <init-param>
            <param-name>ui</param-name>
            <param-value>com.company.mywidgetset.App</param-value>    <!-- use it for testing the widgetset-->
        </init-param>
        <init-param>
            <param-name>widgetset</param-name>
            <param-value>com.company.mywidgetset.MyWidgetset</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyWidgetset</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

项目结构:

|   pom.xml
|
\---src
    +---main
    |   +---java
    |   |   \---com
    |   |       \---company
    |   |           \---mywidgetset
    |   |                   App.java
    |   |                   MyWidgetset.gwt.xml
    |   |
    |   +---resources
    |   |       assembly.xml
    |   |
    |   \---webapp
    |       \---WEB-INF
    |               web.xml
    |
    \---test
        \---java

构建它,使用jar作为项目中的依赖项,然后就完成了。这将永久地让你摆脱GWT称之为“widgetset编译”的巨大痛苦。

答案 1 :(得分:3)

为了减少widget集的编译时间,可以在.gwt.xml文件之后,并取消注释set-property标记。

<!-- Uncomment the following to compile the widgetset for one browser only. 
    This can reduce the GWT compilation time significantly when debugging. The 
    line should be commented out before deployment to production environments. 
    Multiple browsers can be specified for GWT 1.7 as a comma separated list. 
    The supported user agents at the moment of writing were:   ie6,ie8,gecko,gecko1_8,safari,opera 
    The value gecko1_8 is used for Firefox 3 and later and safari is used for 
    webkit based browsers including Google Chrome. -->
<!-- <set-property name="user.agent" value="gecko1_8"/> -->

要创建一个新的vaadin-maven项目,您可以尝试使用vaadin 7插件新项目向导,该向导将创建一个带有常春藤依赖关系管理的新项目,在创建项目后,右键单击项目并删除常春藤依赖关系管理,然后右键单击 - &gt;配置 - &GT;将其转换为maven项目将为项目添加maven依赖管理。

答案 2 :(得分:1)

不要编译任何小部件,使用默认小部件;为此,您必须设置以下文件: web.xml中:

<init-param>
    <name>widgetset</name>
    <value>com.vaadin.DefaultWidgetSet</value>
</init-param>

或者在项目的 UI 实施中:

/**
 * Initial view in Vaadin
 */
@Theme("valo") 
@Widgetset("com.vaadin.DefaultWidgetSet")
public class MainUI extends UI
{

最后在 pom.xml 中只包含这些依赖项:

    <!-- Vaadin dependencies -->
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-server</artifactId>
        <version>${vaadin.version}</version>
    </dependency> 
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-client-compiled</artifactId>
        <version>${vaadin.version}</version> 
    </dependency>   
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-themes</artifactId>
        <version>${vaadin.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version> 
    </dependency> 

没有别的。我解决了这些,我也希望你。

答案 3 :(得分:0)

如果您厌倦了不断编译/重新编译的widgetset, 我建议你使用“CDN Viritin Magic

优点:

  1. @Widgetset
  2. 不需要maven-vaadin-plugin
  3. 以N次为单位减少编译时间。
  4. 注1(适用于Vaadin Spring Boot用户)
    成功的教程演练之后(不要运行mvn clean install!)
    你必须在YourAppNameApplication类中再定义一件事:

    @Bean
    public in.virit.WidgetSet viritinCdnInitializer() {
        return new in.virit.WidgetSet();
    }
    

    注2:可以与spring-boot-maven-plugin一起使用