eclipse+Jettyéçºç°å¢ã®ä½æ
Herokuã«æãåºãã¦ããã¨ãããã®ã®ãeclipse+Jettyã§éçºãããã¨ãå¤ããªãã¾ããã
Jettyã®ãããããã¨ããã¯APIã使ã£ã¦èµ·åç¨ã¯ã©ã¹ãä½ããã¨ã§ãæ®éã®Javaã¢ããªã¨ãã¦èµ·åã§ãããã¨ã§ãã
ããã便å©ãªã¨ããã¯
- èµ·åãéãï¼
- ç¹å¥ãªãã©ã°ã¤ã³ãå ¥ããªãã¦ãããµã¤ã¼ã«eclipseã§ãããã°ã§ãã
- çµã¿è¾¼ã¿ç¨éã«ä½¿ãã
ãªã©ã§ãããã
åããã¨ã¯Glassfishã§ãåºæ¥ããã§ãããJettyã¯ä¾åJARãå°ãªããµã¤ãºãå°ããã®ã§ãæ軽ã§ãããGlassfishã¯APIãå®å®ãã¦ããªãã£ã¦èµ·åç¨ã¯ã©ã¹ã®ä½æã«è¦å´ãããã§ãããã
ãããªããã§ä»åã¯eclipse+Jettyã®ãæ軽éçºç°å¢ãæ§ç¯ããæé ãã¾ã¨ãã¦ã¿ã¾ããã
ä¾ã¨ãã¦ãsample-webãã¨ããããã¸ã§ã¯ããä½ã£ã¦ã¿ã¾ãããã
åæ
- Mavenãå°å ¥æ¸ã¿ã§ãããã¨
ç§ã®ç°å¢ï¼MacOS X Lion 10.7.5ï¼ã«ã¯ããã©ã«ãã§Ver3.0.3ãå ¥ã£ã¦ãã¾ããã
è¨èã«ã¤ãã¦
ãã³ãã³ãã©ã¤ã³ãã¨ããè¨èã¯ãWindowsãªããã³ãã³ãããã³ããããæãMacãªããã¿ã¼ããã«ããæãã¾ãã
ã§ã¯å§ãã¾ãããã
eclipseã¨Mavenãé£æºãããããã®æºå
ã³ãã³ãã©ã¤ã³ã§æ¬¡ã®ã³ãã³ããå®è¡ãã¾ãã
mvn -Declipse.workspace=<eclipseã®workspaceã®ãã¹> eclipse:add-maven-repo
ãã®ã³ãã³ãã§M2_REPOã¨ããå¤æ°ãeclipseã«ä½ããã¾ãã
ã§ãã®ã§ããworkspaceã«å¯¾ãã¦ä¸åº¦ã ãå®è¡ããã°OKã§ãã
ä½æ¥ãã©ã«ãã«ç§»å
ã³ãã³ãã©ã¤ã³ã§æ¬¡ã®ã³ãã³ããå®è¡ãã¾ãã
cd <eclipseããã¸ã§ã¯ããç½®ããã©ã«ã>
eclipseããã¸ã§ã¯ããç½®ããã©ã«ãã¯ãé常ã ã¨workspaceã¨åãå ´æãå¤ãã§ãããã
ããã¸ã§ã¯ãã®ååä½æ
ã³ãã³ãã©ã¤ã³ã§æ¬¡ã®ã³ãã³ããå®è¡ãã¾ãã
mvn archetype:create -DgroupId=jabara -DartifactId=sample-web -DarchetypeArtifactId=maven-archetype-webapp
groupIdã«ã¯é©å½ãªååãæå®ãã¦ä¸ããã
ãã®ã³ãã³ãã§sample-webãã£ã¬ã¯ããªãä½ããããã®ä¸ã«pom.xmlãä½ããã¾ãã
ã³ãã³ãã©ã¤ã³ã§sample-webãã©ã«ãã«ç§»å
ã³ãã³ãã©ã¤ã³ã§æ¬¡ã®ã³ãã³ããå®è¡ãã¾ãã
cd sample-web
ããã¸ã§ã¯ãã«Jettyè¨å®è¿½å
pom.xmlã好ããªã¨ãã£ã¿ã§éãã¦
<!-- çµã¿è¾¼ã¿Jettyã®å®è¡ã«å¿ è¦ --> <dependency> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>jetty-all-server</artifactId> <version>8.1.4.v20120524</version> </dependency> <dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jsp-2.1-glassfish</artifactId> <version>2.1.v20100127</version> </dependency> <!-- çµã¿è¾¼ã¿Jettyã«ServletAPI3.0ã®ã¢ããã¼ã·ã§ã³ã解éãããããã«å¿ è¦ --> <dependency> <groupId>asm</groupId> <artifactId>asm-commons</artifactId> <version>3.3.1</version> </dependency>
ããã¸ã§ã¯ãã®Javaã®ãã¼ã¸ã§ã³ã6ã«ãã
pom.xmlã®
<plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins>
ãã¼ã¸ã§ã³ã7ã«ãããå ´åã¯ã1.6ããã1.7ãããã°OKã§ãã
eclipseããã¸ã§ã¯ãã®è¨å®ã追å
ã³ãã³ãã©ã¤ã³ã§æ¬¡ã®ã³ãã³ããå®è¡ãã¾ããããã§eclipseã«ã¤ã³ãã¼ãã§ããããã«ãªãã¾ãã
mvn eclipse:eclipse
ååã¯ããããªã©ã¤ãã©ãªããã¦ã³ãã¼ãããã®ã§æéããããã¾ã
ããã¸ã§ã¯ããeclipseã«ã¤ã³ãã¼ã
eclipseãèµ·åããã¡ãã¥ã¼ã®ããã¡ã¤ã«ãâãã¤ã³ãã¼ããâãæ¢åããã¸ã§ã¯ããã¯ã¼ã¯ã¹ãã¼ã¹ã¸ãã使ã£ã¦sample-webãã©ã«ããã¤ã³ãã¼ããã¾ãã
詳ããæé ã¯æ¬¡ã®ãµã¤ããåèã«ãã¦ä¸ããã
http://www.javadrive.jp/eclipse3/project/index4.html
ã½ã¼ã¹ãã©ã«ããä½æ
ãªããã½ã¼ã¹ãã©ã«ãã足ãã¾ãããããã§ã¯Jettyã®èµ·åç¨ã¯ã©ã¹ãä½ããªãã®ã§ãä½æãã¾ãã
ãsrc/main/javaããã©ã«ããã½ã¼ã¹ãã©ã«ãã¨ãã¦ä½æãã¦ä¸ããã
ããã©ã«ãåºåãã©ã«ãã®å¤æ´
mvnã®è¨å®ããã®ã¾ã¾ä½¿ãã¨@WebServletï¼ãªã©ï¼ã®ä»ããã¯ã©ã¹ãèªåçã§æ¢ãæ©è½ãåä½ãã¾ããã
ããã§ãsample-web/src/main/webapp/WEB-INF/classesãã«å¤æ´ãã¾ãã
ãªãããã®è¨å®ã¯
mvn eclipse:eclipse
ã³ãã³ããå®è¡ãã度ã«æ½ãå¿
è¦ãããã¾ããä¾åã©ã¤ãã©ãªã追å ããã¨ããªã©ããã®ã³ãã³ããå®è¡ãããã¨ã¯ããããã¾ãããã®æã«ãã®è¨å®ãå¿ããã¨Webã¢ããªãåä½ããªããªãã®ã§ã注æãå¿
è¦ã§ãã
ãã£ã¨è¯ãæ¹æ³ããã£ã¨ããã¨æãã®ã§ãããã¾ã 調ã¹ãã¤ãã¦ãã¾ããã
ããã¾ã§ã®è¨å®ã®ã¹ã¯ãªã¼ã³ã·ã§ããã§ãã
Jettyèµ·åç¨ã¯ã©ã¹ä½æ
src/main/javaç´ä¸ã«æ¬¡ã®ã¯ã©ã¹ãä½ãã¾ãã
import org.eclipse.jetty.annotations.AnnotationConfiguration; import org.eclipse.jetty.plus.webapp.EnvConfiguration; import org.eclipse.jetty.plus.webapp.PlusConfiguration; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.FragmentConfiguration; import org.eclipse.jetty.webapp.MetaInfConfiguration; import org.eclipse.jetty.webapp.TagLibConfiguration; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.webapp.WebInfConfiguration; import org.eclipse.jetty.webapp.WebXmlConfiguration; /** * @author jabaraster */ public class SampleWebStarter { /** * @param pArgs - * @throws Exception - */ public static void main(final String[] pArgs) throws Exception { final int port = getWebPort(); final String webappDirLocation = "src/main/webapp/"; //$NON-NLS-1$ final Server server = new Server(port); final WebAppContext context = new WebAppContext(); context.setConfigurations(new Configuration[] { // new AnnotationConfiguration() // , new WebXmlConfiguration() // , new WebInfConfiguration() // , new TagLibConfiguration() // , new PlusConfiguration() // , new MetaInfConfiguration() // , new FragmentConfiguration() // , new EnvConfiguration() // }); context.setContextPath("/"); //$NON-NLS-1$ context.setDescriptor(webappDirLocation + "/WEB-INF/web.xml"); //$NON-NLS-1$ context.setResourceBase(webappDirLocation); context.setParentLoaderPriority(true); server.setHandler(context); server.start(); server.join(); } private static int getWebPort() { final String webPort = System.getenv("PORT"); //$NON-NLS-1$ if (webPort == null || webPort.isEmpty()) { return 8080; } return Integer.parseInt(webPort); } }
ãã¼ãã®æ±ºå®ã«ã¯ä¸å·¥å¤«ãã¦ãã¾ãã
Herokuã§åä½ããããã¨ãèæ
®ããç°å¢å¤æ°PORTãè¦ãããã«ãã¦ãã¾ããããç°å¢å¤æ°PORTã空ãªãã8080ã決ãæã¡ã§ä½¿ãã¾ãã
ãªãã¯ã©ã¹ã®package宣è¨ãçç¥ãã¦ããï¼default packageã«æå±ããã¦ããï¼ã®ã«ã¯æ·±ãæå³ã¯ããã¾ããã
Jettyèµ·å
SampleWebStarterãeclipseããèµ·åãã¾ãã
ï¼ç§ãããã§èµ·åãã¾ããéãã
åä½ç¢ºèª
ãã©ã¦ã¶ã§æ¬¡ã®URLã«ã¢ã¯ã»ã¹ãã¾ãã
http://localhost:8080/
ãHello World!ãã表示ãããã°æåï¼
Servletãä½ã£ã¦ã¿ã
ã¤ãã§ã«ServletAPI3.0ã使ã£ã¦Servletãä½ã£ã¦ã¿ã¾ãããã
src/main/javaã®ä¸ã«æ¬¡ã®Javaã¯ã©ã¹ãä½ãã¾ãã
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns={"/sample"}) public class SampleServlet extends HttpServlet{ protected void doGet(final HttpServletRequest pReq, final HttpServletResponse pResp) throws ServletException, IOException { pResp.setContentType("text/plain"); pResp.getWriter().print("Hello World!"); } }
ãããã®Servletã®ã½ã¼ã¹ã«ãSyntax error, annotations are only available if source level is 1.5 or greaterãã¨ããã¨ã©ã¼ãèµ·ããå ´åããããã¸ã§ã¯ãã®Javaã®ãã¼ã¸ã§ã³ã6ã«ãããã®æé ã®è¨å®ã追å ãã¦ä¸ããã
Servletã®åä½ç¢ºèª
ãã©ã¦ã¶ã§æ¬¡ã®URLã«ã¢ã¯ã»ã¹ãã¾ãã
http://localhost:8080/sample
ãHello World!ãã表示ãããã°æåï¼