开发者

Using Cofoja with Wicket (or even with just Maven)

开发者 https://www.devze.com 2023-04-08 02:54 出处:网络
I am trying my darnedest to get Google Cofoja to run in my Apache Wicket application which uses Maven2 as seems to be standard.

I am trying my darnedest to get Google Cofoja to run in my Apache Wicket application which uses Maven2 as seems to be standard.

The project was initially generated using Leg Up with the Archetype "Wicket 1.4.12, Guice 2.0, WarpPersist 2.0 (snapshot), Hibernate 3.5.6" selected.

What I've tried most recently (and seems to be the closest to working) is building with maven (which i've managed to make build the contract classes), and then attempting to run the project using ant to get the contract checks to happen. The most current problem is I am not sure what to make my main class. I've tried making it the class that runs the jetty server, but I get a class not found exception.

J:\adminconsole>ant run
Buildfile: J:\adminconsole\build.xml

run:
     [java] java.lang.NoClassDefFoundError: com/mycompany/myproject/adminconsole/Start
     [java] Caused by: java.lang.ClassNotFoundException: com.mycompany.myproject.adminconsole.Start
     [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
     [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
     [java] Could not find the main class: com.mycompany.myproject.adminconsole.Start. Program will exit.
     [java] Exception in thread "main"
     [java] Java Result: 1

BUILD SUCCESSFUL
Total time: 2 seconds

As well, even if I did get this solution working, this is far from the ideal solution of having the contract checks happen as part of the maven build.

Now, what I have currently:

pom.xml

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.myproject</groupId>
    <artifactId>adminconsole</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <jdk.version>1.5</jdk.version>

        <slf4j.version>1.5.11</slf4j.version>
        <wicket.version>1.4.12</wicket.version>
        <jetty.version>6.1.25</jetty.version>
        <cofoja.version>1.0</cofoja.version>
        <asm.version>3.3.1</asm.version>
    </properties>
    <name>Admin Console New</name>
<!--
    <repositories>
        <repository>
            <id>jboss</id>
            <name>JBoss Repository</name>
            <url>https://repository.jboss.org/nexus/content/groups/public/</url>
        </repository>
    </repositories>
-->
    <build>
        <finalName>fsrpadmin</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/java</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
            <testResource>
                <directory>src/test/resources</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.mycompany.myproject.adminconsole.Start</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <targetJdk>${jdk.version}</targetJdk>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <configuration>
                    <xmlOutput>true</xmlOutput>
                    <effort>Max</effort>
                    <threshold>Low</threshold>
                </configuration>
                <version>2.3.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.8</version>
            </plugin>

            <!-- Run annotation processors on src/main/java sources -->
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <outputDirectory>target\classes</outputDirectory>
                            <processors>
                                <processor>com.google.java.contract.core.apt.AnnotationProcessor</processor>
                            </processors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Run annotation processors on src/test/java sources -->
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <executions>
                    <execution>
                        <id>process-test</id>
                        <goals>
                            <goal>process-test</goal>
                        </goals>
                        <phase>generate-test-sources</phase>
                        <configuration>
                            <outputDirectory>target\classes</outputDirectory>
                            <processors>
                                <processor>com.google.java.contract.core.apt.AnnotationProcessor</processor>
                            </processors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <!--  WICKET DEPENDENCIES -->
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket</artifactId>
            <version>${wicket.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-guice</artifactId>
            <version>${wicket.version}</version>
        </dependency>

        <!-- LOGGING DEPENDENCIES - LOG4J -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>1.8.0.10</version>
            <scope>runtime</scope>
        </dependency>

        <!-- You will need to install this yourself from http://warp-persist.googlecode.com/svn/trunk/warp-persist/dist/ -->
        <dependency>
            <groupId>com.wideplay.warp</groupId>
            <artifactId>warp-persist</artifactId>
            <version&开发者_StackOverflowgt;2.0-20090214</version>
        </dependency>

        <!-- Cofojo - Contracts for Java LOCALLY INSTALLED from http://code.google.com/p/cofoja/downloads/list -->
        <dependency>
            <groupId>com.google</groupId>
            <artifactId>cofoja</artifactId>
            <version>${cofoja.version}</version>
        </dependency>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm-all</artifactId>
            <version>${asm.version}</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.8.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty</artifactId>
            <version>${jetty.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Other -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>1.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="adminconsole-run" default="package" basedir=".">

<!-- ====================================================================== -->
<!-- Build environment properties                                           -->
<!-- ====================================================================== -->

<property file="${user.home}/.m2/maven.properties"/>
<property file="maven-build.properties"/>

<!-- ====================================================================== -->
<!-- Run Contracts target                                                   -->
<!-- ====================================================================== -->
<target name="run">
    <java jar="${maven.build.dir}/${maven.build.finalName}.war" fork="true">
        <jvmarg value="-javaagent:lib/cofoja/cofoja-1.0-r138.jar"/>
    </java>
</target>

</project>

maven-build.properties

maven.build.finalName=fsrpadmin
maven.build.dir=target

com/mycompany/myproject/adminconsole/Start.java

package com.marsh.fsrp.adminconsole;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class Start {

    public static void main(String[] args) throws Exception {
        Server server = new Server();
        SocketConnector connector = new SocketConnector();
        connector.setPort(8080);
        server.setConnectors(new Connector[] { connector });

        WebAppContext bb = new WebAppContext();
        bb.setServer(server);
        bb.setContextPath("/");
        bb.setWar("src/main/webapp");


        // START JMX SERVER
        // MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        // MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer);
        // server.getContainer().addEventListener(mBeanContainer);
        // mBeanContainer.start();

        server.addHandler(bb);

        try {
            System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
            server.start();
            while (System.in.available() == 0) {
                Thread.sleep(5000);
            }
            server.stop();
            server.join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(100);
        }
    }
}

If you know how to fix my current ant problem, or even better, have an idea of how to scrap the ant-hack all together, that would be super exciting (:

Update 1

Removed -'s from code as that is bad (thanks @Martijn Dashorst). Sadly, this didn't fix the problem.


Don't use a- in your artifact and group IDs. They are invalid Java identifier characters. The script uses the groupId and artifactId to generate the package name in your project.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号