Objective
goal of this article: use Eclipse to build a maven-based Web project for the IDE environment , explain the build process, project structure, program run, Debug and test process, and use maven as a continuous integration tool.
??
Object-oriented: a . NET developer who transforms Java
??
knowledge Point:eclipse,maven,jfinal,jetty,tomcat,junit
??
Environment preparation
Maven
- Install maven-3.3.9, recommended 3.0+ version
- Check the installation version, command line input : mvn-version
??
??
??
Tomcat
- Download TOMCAT-V7, unzip to local.
??
Eclipse
- Install the Eclipse-neon version. Make sure that the plugins in the diagram are installed.
??
- Configure Maven
??
- Configure the server
??
??
??
??
Create a project
Use Eclipse to create a MAVEN-based Web project
Slightly
To create a Web project on the command line using MAVEN
??
- To Create a Web project:
- Execute command : mvn archetype:generate, the console will not respond after displaying the following message (cause :mvn try to list all the project templates for the server, Too many types and too slow access to Maven repositories, why too slow? You know, the Great Firewall .
??
??
- Adding parameters makes MVN to list only the native templates: -darchetypecatalog=internal
- template is still a lot, it is recommended to add parameters, such as to create Web items, available parameters -dfilter=web
- Once you have determined the template type, you can specify the template directly in the parameter: -darchetypeartifactid=maven-archetype-webapp
- MVN The interactive mode is used by default, and parameters are specified as non-interactive mode: -dinteractivemode=false
- Create a Web site project ( package name Com.demo, project name Websample) The complete command: MVN Archetype:generate-darchetypeartifactid=maven-archetype-webapp-dgroupid=com.demo-dartifactid=websample- Dinteractivemode=false-darchetypecatalog=internal
??
??
- viewing compilation, testing, packaging effects , Command : mvn compile test Package
- Generate the relevant files (. project,.classpath) for the Eclipse Project,
- to enter the project directory, command: CD Websample
- Execute command: mvn eclipse:eclipse
- Other
- Most of the online cases did not set the parameters -darcheypecatalog, due to national conditions, the basic card in a).
- Set the-darchetypecatalog parameter even if the template is specified , otherwise it will be stuck in a).
Open Project in Eclipse
Importing Maven projects in eclipse
Maven builds project files after importing
- Generate the relevant files (. project,.classpath) for the Eclipse Project,
- to enter the project directory, command: CD Websample
- Execute command: mvn eclipse:eclipse
- Eclipse Import in
??
??
- compiler error handling , Makers window prompt error
- the superclass " Javax.servlet.http.HttpServlet "wasn't found on the Java Build Path
workaround :
- right click on Project
- select Properties
- select Project Facets
- select Apache Tomcat as Runtime server
- click OK
Precautions
- Import Project (Java World called Widget ) The name cannot be the same as an existing artifact in the workspace. (artifactid node value in pom.xml)
??
Source Code Development
Refer to the Jfinal manual to add the MVC structure source code.
- Modify the project compilation output directory to src/main/webapp/web-inf/classes(maven default is target/classes)
- Modify the Pom. xml file to add dependencies.
- Modify the Web. xml file to specify a filter.
- adding Java files
- Create a Java folder (the source location in the MAVEN structure is Src/main/java).
- Add configuration file ,Democonfig.java file, inherit jfinalconfig.
- Add controller files, Hellocontroller.java files.
??
Run debugging
There are 2 modes of operation in the jfinal manual
- App runs "Java application"
- This approach relies on jetty, a lightweight, embedded Servle container.
- Deploy to server to run the "Run on server"
- It is recommended that you use the Tomcat directory configuration directly, and note that modifying the port does not cause it to start
??
??
jfinal Test Extension Jfinal-ext
- the extension does not support jfinal-2.1+, which modifies the logger class in the version refactoring, and other incompatibilities are not known.
- Open source software upgrade compatibility is poor
??
Writing unit tests using JUnit
Junit3 vs JUNIT4
- Maven template creation project in default dependent JUNIT3
- The default unit test plug-in for Eclipse uses JUNIT4
- Junit3.8 Requirements for test code
- public.
- void.
- No method parameters.
- Method name must begin with test
- The difference between the two is very large, V4 uses a more concise annotation feature (JAVA5) to simplify the test code
Add test Code
JUNIT3 Test Code
Import Junit.framework.TestCase;
Import static org.junit.assert.*;
public class Addoperationtest extends testcase{
public void SetUp () throws Exception {
}
public void TearDown () throws Exception {
}
public void Testadd () {assertequals (Expresult, result);}
}
JUNIT4 Test Code
Import Junit.framework.TestCase;
Import Org.junit.After;
Import Org.junit.Before;
Import Org.junit.Test;
Import static org.junit.assert.*;
public class Addoperationtest extends testcase{
@before
public void SetUp () throws Exception {}
@after
public void TearDown () throws Exception {}
@test
public void Testadd () {assertequals (Expresult, result);}
}
??
unit tests in maven,
- Command : mvn test
??
unit tests in Eclipse
- The default output folder for Java Build Path modified in the above steps is src/main/webapp/web-inf/classes, where you need to modify the test source directory to output it to the target/ Test-classes, maintaining consistency with Maven
??
??
- New configuration file
- Modify Classpath, or you will be prompted when you run the unit test:eclipse-java.lang.classnotfoundexception
- Open Your Run configurations
- Click on the junit-test want to start
- Go to the Classpath tab
- Try to add a folder (click on user entries, click on Advanced, click on Add Folders, click on OK and search the Outputfold Er for your test classes (those you find under Projektproperties Java build path, source))
??
??
Web Program Packaging
- command: MVN Package
- The above command will output the Websample.war and Websample folders in the target/directory.
- Websample.war contains all the files required for the project
??
Continuous integration
Continuous integration practices
- Maintain only one source repository
- Automated Build
- Let build self-test
- Each person submits code to the backbone every day
- Each commit should build the backbone on the continuous integration machine
- Maintain a fast build
- Testing in a simulated production environment
- Make it easy for everyone to get the latest executables
- Everyone can see the progress.
- Automating deployment
??
Extended Reading
The relationship between Maven and eclipse
??
??
??
Resources
- Creating Simple Web application Using Apache Maven
- How to use Maven Creating a Web project (concrete steps)
- Convert a maven-generated Java project into a project that supports the Eclipse IDE
- Using Maven in Eclipse
- MAVEN-based Continuous integration practices
- JUNIT4 detailed
Java Web Project