简体   繁体   English

尝试使用java和clojure在eclipse中创建maven动态Web项目

[英]Trying to create maven dynamic web project in eclipse with java and clojure

I have created maven-archetype-webapp project in Eclipse. 我在Eclipse中创建了maven-archetype-webapp项目。 And I want to use Clojure code in Java. 我想在Java中使用Clojure代码。 So, there is my core.clj file: 所以,有我的core.clj文件:

(ns coq.core
 (:gen-class
   :name coq.core
    :methods [#^{:static true} [foo [int] void]]))

(defn -foo [i] (println "Hello from Clojure. My input was " i))

(defn -main [] (println "Hello from Clojure -main." ))

pom.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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>gleboGroup</groupId>
<artifactId>javaandclojure</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>javaandclojure Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1-b02</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.clojure</groupId>
        <artifactId>clojure</artifactId>
        <version>1.3.0</version>
    </dependency>
</dependencies>
<build>
    <finalName>javaandclojure</finalName>
    <plugins>
        <plugin>
            <groupId>com.theoryinpractise</groupId>
            <artifactId>clojure-maven-plugin</artifactId>
            <version>1.3.10</version>
            <configuration>
                <sourceDirectories>
                    <sourceDirectory>src/main/clojure</sourceDirectory>
                </sourceDirectories>
                <testSourceDirectories>
                    <testSourceDirectory>src/test/clojure</testSourceDirectory>
                </testSourceDirectories>
            </configuration>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <executions>
                <execution>
                    <id>default-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-testCompile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

There is my single servlet with error on core.foo(123): 我的单个servlet在core.foo(123)上有错误:

package com.javaandclojure;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import coq.*;

@WebServlet({ "/*"})
public class Main extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        System.out.println("inside get!");
        core.foo(123);// core cannot be resolved
        request.setAttribute("number", 42);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

Some scrinshots from eclipse: 日食的一些暗影:

在此输入图像描述

build path: 构建路径:

在此输入图像描述

Maven Build with goals:" clean package" gives it: Maven构建目标:“干净的包”给它:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building javaandclojure Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ javaandclojure ---
[INFO] Deleting D:\porn\indigo\javaandclojure\target
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ javaandclojure ---
[WARNING] Using platform encoding (Cp1251 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ javaandclojure ---
[WARNING] File encoding has not been set, using platform encoding Cp1251, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\porn\indigo\javaandclojure\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] \porn\indigo\javaandclojure\src\main\java\com\javaandclojure\Main.java:[10,10] error: package coq does not exist
[ERROR] \porn\indigo\javaandclojure\src\main\java\com\javaandclojure\Main.java:[17,2] error: cannot find symbol
[INFO] 2 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.042s
[INFO] Finished at: Sun Nov 11 16:28:25 EET 2012
[INFO] Final Memory: 8M/20M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project javaandclojure: Compilation failure: Compilation failure:
[ERROR] \porn\indigo\javaandclojure\src\main\java\com\javaandclojure\Main.java:[10,10] error: package coq does not exist
[ERROR] \porn\indigo\javaandclojure\src\main\java\com\javaandclojure\Main.java:[17,2] error: cannot find symbol
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Maybe I should have used another maven archetype? 也许我应该使用另一个maven原型?

That is because the Java compiler is being run before the clojure compiler. 那是因为Java编译器在clojure编译器之前运行。

If you force the Java compilation to take place after the clojure-maven-plugin it will work. 如果强制在clojure-maven-plugin之后进行Java编译,它将起作用。 Add these lines after the clojure-maven-plugin : clojure-maven-plugin 之后添加这些行:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <executions>
        <execution>
            <id>default-compile</id>
            <phase>none</phase>
        </execution>
        <execution>
            <id>default-testCompile</id>
            <phase>none</phase>
        </execution>
        <execution>
            <id>java-compile</id>
            <phase>compile</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
        <execution>
            <id>java-testCompile</id>
            <phase>test-compile</phase>
            <goals>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

I would suggest that you create a project with 2 modules so it is clear what depends on what. 我建议您创建一个包含2个模块的项目,这样很明显取决于什么。 If your Java code has to depend on your Clojure code, then the java module (war?) should depend on the clojure module. 如果你的Java代码必须依赖于你的Clojure代码,那么java模块(war?)应该依赖于clojure模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM