Social networks
You can find me on:
   
Open sharing content

These articles are available under Creative Commons license BY-SA-3.0

Posts Tagged ‘eclipse’

The following steps will allow you to develop and deploy a first demo web-app with Glassfish Server 4.
1) To install Glassfish 4.0 follow the steps at http://glassfish.java.net/download.html
For example, you can get the zip version and extract into: C:\glassfish4.

2) If you´re working on a Windows Desktop Rename glassfish4/glassfish/bin asadmin to *.s to prevent the following Maven error:
Unable to start domain "domain1". IOException: Cannot run program"C:\glassfishv3\glassfish\bin\asadmin": CreateProcess error=193, %1 is not a valid Win32-Application

In this way The system will run asadmin.bat, instead of calling the Linux script (provided with no file extension by Glassfish).

3) On Eclipse install Glassfish plugin (https://marketplace.eclipse.org/content/glassfish-tools-kepler) to manage the server graphically. Otherwise you would not be able to installing with the command File>New>Other>Server> etc.
Starting Glassfish for the first time, you will have a user called “admin” with no password. You can manage Glassfish with Admin-Console available at: http://localhost:4848/ or even with asadmin CLI console (under C:\glassfish4\glassfish\bin).

4) To deploy your application using Maven, create the settings.xml file under the .m2 folder (for example, under Windows at the location: C:\Users\username\.m2), providing the glassfish profile credentials: they will automatically imported in the pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <profiles>
        <profile>
            <id>glassfish-context</id>
            <properties>
                <local.glassfish.home>C:\\glassfish4\\glassfish</local.glassfish.home>
                <local.glassfish.user>admin</local.glassfish.user>
                <local.glassfish.domain>domain1</local.glassfish.domain>
                <local.glassfish.passfile>
            ${local.glassfish.home}\\domains\\${local.glassfish.domain}\\config\\domain-passwords
                </local.glassfish.passfile>
            </properties>
        </profile>
    </profiles>
 
    <activeProfiles>
        <activeProfile>glassfish-context</activeProfile>
    </activeProfiles>
</settings>

5) Create a new Maven project with the maven archetype “maven-archetype-webapp” template. As a default you can already find the index.jsp HelloWorld page.

6) Add the following plugins to the pom.xml:

…
<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<failOnMissingWebXml>false</failOnMissingWebXml>
					<!-- <webXml>src\main\webapp\WEB-INF\web.xml</webXml> -->
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.glassfish.maven.plugin</groupId>
				<artifactId>maven-glassfish-plugin</artifactId>
				<version>2.1</version>
				<configuration>
				<glassfishDirectory>${local.glassfish.home}</glassfishDirectory>
					<user>admin</user>
					<passwordFile>${local.glassfish.passfile}</passwordFile>
					<domain>
						<name>domain1</name>
						<httpPort>8080</httpPort>
						<adminPort>4848</adminPort>
					</domain>
					<components>
						<component>
							<name>${project.artifactId}</name>
					<artifact>target/${project.build.finalName}.war</artifact>
						</component>
					</components>
					<debug>true</debug>
					<terse>false</terse>
					<echo>true</echo>
				</configuration>
			</plugin>

		</plugins>
		<finalName>Glassfish_HelloWorld</finalName>
	</build>
</project>

7) Launch the maven command:
clean package glassfish:deploy

You may find errors related to the Dynamic Web Module in the Eclipse project, even if the maven gives you “BUILD SUCCESS”. For now it´s ok.

With the given pom.xml the application will be deployed into:
C:\glassfish4\glassfish\domains\domain1\applications

You can see it up and running at: http://localhost:8080/Glassfish_HelloWorld/

You can manage the applications, for example editing the Content Root folder, directly in the administration console.

Demo Code available at:
http://sourceforge.net/p/mavendemo/code/HEAD/tree/GlassfishHelloWorld/

If you want to geenrate JAXB classes from a xsd schema in a Maven project, you need to specify the dependencies for jaxb and the plugin.

The pom.xml file is like the following:

<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>
	<groupId>de.demo.maven.jaxb</groupId>
	<artifactId>MAVEN_JAXB_DEMO</artifactId>
	<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.5</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.5</version>
</dependency>
</dependencies>
	
 <build>
    <plugins>
        <!-- JAXB xjc plugin that invokes the xjc compiler to compile XML schema into Java classes.-->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- The schema directory or xsd files. -->
                <schemaDirectory>${basedir}/src/main/resources</schemaDirectory>
                <!-- The package in which the source files will be generated. -->
                <packageName>de.demo.team</packageName>
                <!-- The working directory to create the generated java source files. -->
                <outputDirectory>${basedir}/src/main/java</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
  </build>
	
</project>

in the configuration tags you need to specify “schemaDirectory” (the location of the .xsd file) and in the “outputDirectory” the destination folder for the generated classes.

First of all you need to run the maven compile command. Then you can generate the JAXB classes: in eclipse, for example, you can right-click on the project and run New>Other>JAXB>”JAXB Classes from a Schema”.
Of course you need to add the Eclipse plugin first.

Eclipse won´t generate automatically the @XmlRootElement annotation. You will have to add it on your own, like this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MBean", propOrder = {
    "attribute"
})
@XmlRootElement
public class MBean {
...
}