Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.airhacks</groupId>
<artifactId>wad</artifactId>
<version>0.1.1</version>
<version>0.1.2</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
Expand Down Expand Up @@ -74,4 +74,4 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
</project>
59 changes: 59 additions & 0 deletions src/main/java/com/airhacks/wad/control/ThinWarNameProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.airhacks.wad.control;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public interface ThinWarNameProvider {

public static String getCurrentThinWarName(Path pom) throws IOException {
String thinWARName = getNameByDefaultConvention();

String finalName = getNameByFinalName(pom);

if (finalName != null && finalName.trim().length() > 0) {
thinWARName = finalName + ".war";
}

return thinWARName;
}

static String getNameByFinalName(final Path pom) throws IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
String result;

try {
DocumentBuilder builder = factory.newDocumentBuilder();
File pomXml = pom.toFile();
Document document = builder.parse(new InputSource(new FileReader(pomXml)));
XPath xPath = XPathFactory.newInstance().newXPath();

result = (String) xPath.evaluate("/project/build/finalName", document, XPathConstants.STRING);
} catch (ParserConfigurationException | SAXException | XPathExpressionException e) {
return null;
}

return result;
}

static String getNameByDefaultConvention() {
Path currentPath = Paths.get("").toAbsolutePath();
Path currentDirectory = currentPath.getFileName();
return currentDirectory + ".war";
}


}
12 changes: 8 additions & 4 deletions src/main/java/wad/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import com.airhacks.wad.boundary.WADFlow;
import com.airhacks.wad.control.Configurator;
import static com.airhacks.wad.control.PreBuildChecks.pomExists;
import static com.airhacks.wad.control.PreBuildChecks.validateDeploymentDirectories;
import com.airhacks.wad.control.ThinWarNameProvider;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -16,6 +16,9 @@
import java.util.Set;
import java.util.stream.Collectors;

import static com.airhacks.wad.control.PreBuildChecks.pomExists;
import static com.airhacks.wad.control.PreBuildChecks.validateDeploymentDirectories;

/**
*
* @author airhacks.com
Expand Down Expand Up @@ -61,8 +64,9 @@ public static void main(String[] args) throws IOException {
}
pomExists();
Path currentPath = Paths.get("").toAbsolutePath();
Path currentDirectory = currentPath.getFileName();
String thinWARName = currentDirectory + ".war";
Path pomXml = currentPath.resolve("pom.xml");

String thinWARName = ThinWarNameProvider.getCurrentThinWarName(pomXml);

Path thinWARPath = Paths.get("target", thinWARName);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.airhacks.wad.control;

import org.junit.Test;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

public class ThinWarNameProviderTest {

@Test
public void finalNameInPomIsNotSet() throws IOException {
Path pomWithoutFinalNamePath = Paths.get("src/test/resources", "pom-without-final-name.xml");
Path currentPath = Paths.get("").toAbsolutePath();
Path currentDirectory = currentPath.getFileName();
String thinWARName = currentDirectory + ".war";

String thinWarName = ThinWarNameProvider.getCurrentThinWarName(pomWithoutFinalNamePath);
assertThat(thinWarName, equalTo(thinWARName));
}

@Test
public void finalNameInPomIsSet() throws IOException {
Path pomWithFinalNamePath = Paths.get("src/test/resources", "pom-with-final-name.xml");

String thinWarName = ThinWarNameProvider.getCurrentThinWarName(pomWithFinalNamePath);
assertThat(thinWarName, equalTo("test.war"));
}
}
24 changes: 24 additions & 0 deletions src/test/resources/pom-with-final-name.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<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>com.airhacks</groupId>
<artifactId>test</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>test</finalName>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
</project>
24 changes: 24 additions & 0 deletions src/test/resources/pom-without-final-name.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<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>com.airhacks</groupId>
<artifactId>test</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
</project>