<target>标签和web程序引用问题

本文介绍如何利用MSBuild的&lt;target&gt;标签解决Web项目中外部DLL的引用问题,包括自定义编译流程、调整任务执行顺序及使用Copy、MakeDir等任务。

有时候,我们会发现不得不用MSBuild进行生成项目,这时会抱怨,MSBuild真是不智能,特别是引用问题。当我们

使用web项目,并且,这个项目使用了外部生成好的dll,这个时候,你就会发现,web项目是没有工程文件的,也就

意味着你没法引用外部生成的dll,这时你会想,把这个引用到其他的层,但是你会失望的发现,为什么还是不行,这

dll还是没有生成到webbin目录下。MSBuild编译始终会报引用问题。

那么,问题就来了,这该如何做能,这时候,我推荐使用项目工程文件中的<target>标签。

当你打开一个项目的工程文件时,你应该发现如下:

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets. -->
  <Target Name="BeforeBuild">  
  </Target>
  <Target Name="AfterBuild">
  </Target>

注意,这个是系统生成的,默认是把这个注释掉的,需要手动修改掉注释,<target>任务,即之前说MSBuild中的目标,这里默认是有两个的,BeforeBuild 顾名思义,这个表示在MSBuild调用之前,会先调用这个节点中的所有的节点任务,而AfterBuild则相反,这个就表示在MSBuild编程生成成功后才会进行调用

这时候会发现,当有多个target的时候,就会出现调用的问题,默认是从上往下的,当时如果你想有自己的调用方式,这时候你可以如下:

<Target Name=”CheckOut” ></Target>
<Target Name=”Build”  DependsOnTargets=”CheckOut”> 
    <Task Name=”Build”.../> 
    <Task Name=”UnitTest” ... />
</Target>
<Target Name=”CheckIn” DependsOnTargets=”CheckOut;Build”> 
</Target>

在上面的demo中,一共有三个taeget,出现这样,这时候如果你想决定他们的调用顺序,这就需要用到DependsOnTargets属性,这个表示当当前目标调用前,要先保证这个属性中的target先被调用过才能运行当前的target,上面的demo就是说当CheckIn调用前,一定要保证CheckOut和Build先被调用过。

在这里,MSBuild附带了很多的任务,比如复制(Copy)这个就用于解决编译的引用问题,用于创建目录的MakeDir以及用于VS编译的Csc

1,Copy 这个方法就是用于文件的复制,这个就可以解决引用问题,原理是,在MSBuild编译结束后,把缺少的dll通过复制代码复制到需要的bin目录下

          <CopySourceFiles="%(Reference.HintPath)"  DestinationFolder="..\Web\Bin" />

CopySourceFiles表示要被复制的源文件,可以使用@(ItemType)找到对应的节点,如@(Reference),就表示在工程文件中的Reference节点,当需要找对应节点的子节点时需要使用%(Reference.HintPath),这个表示在Reference节点下的HintPath节点,DestinationFolder这个表示目标路径,是指需要把文件复制到地方,即想要复制的web次层的bin目录下,相对路径,使用就是把这个加到对应需要复制的引用了dll的那个层的工程文件中,加到

  <Target Name="AfterBuild">
  </Target>
这个节点中间,这时候就会在生成成功后,把对应的dll复制到指定的bin

2,MakeDir这个用于创建文件夹

<PropertyGroup>
        <OutputDirectory>\Output\</OutputDirectory>
</PropertyGroup>

<Target Name="CreateDirectories">
        <MakeDir Directories="$(OutputDirectory)"/>
</Target>

这里就是在目录下创建Output目录

3,Csc用于对于一个项目的编译生成,官方给的demo,学习过工程文件的基本上都知道下面节点的意思,我就不一一介绍了

<Target Name="Compile" DependsOnTarget="Resources" >
    <Csc Sources="@(CSFile)"
          TargetType="library"
          Resources="@(CompiledResources)"
          EmitDebugInformation="$(includeDebugInformation)"
          References="@(Reference)"
          DebugType="$(debuggingType)" >
        <Output TaskParameter="OutputAssembly"
                  ItemName="FinalAssemblyName" />
    </Csc>
</Target>







运行显示D:\SE-master\Soe\src\main\java\com\se\controller\ClassController.java:7:52 java: 程序包org.springframework.beans.factory.annotation不存在 pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <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.se</groupId> <artifactId>deep</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>deep Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <!-- spring版本号 --> <spring.version>5.1.9.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.5.2</mybatis.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.2</version> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.7.0</version> </dependency> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- spring Data Redis--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.3.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.2</version> </dependency> <!-- log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.28</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.9</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.9</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- 文件上传jar包--> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> </dependencies> <build> <finalName>deep</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> <!-- 编译xml文件properties文件--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project> 有什么问题
最新发布
12-01
<?xml version="1.0" encoding="UTF-8"?> <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.se</groupId> <artifactId>deep</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>deep Maven Webapp</name> <url>http://www.example.com</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>1.8</java.version> </properties> <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Starter Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Commons BeanUtils --> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> </dependency> <!-- Druid 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <!-- 或其他兼容版本 --> <scope>provided</scope> </dependency> <!-- Servlet API --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <!-- Spring Boot Starter JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> <build> <finalName>deep</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 这里面有什么问题
12-01
<%-- Created by IntelliJ IDEA. User: vili Date: 2019/8/23 Time: 13:42 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@page isELIgnored="false" %> <!--header--> <div class="header"> <div class="container"> <nav class="navbar navbar-default" role="navigation"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <h1 class="navbar-brand"><a href="index.action">叮当书城</a></h1> </div> <!--navbar-header--> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="index.action" <c:if test="${param.flag==1}">class="active"</c:if>>首页</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle <c:if test="${param.flag==2}">active</c:if>" data-toggle="dropdown">商品分类<b class="caret"></b></a> <ul class="dropdown-menu multi-column columns-2"> <li> <div class="row"> <div class="col-sm-12"> <h4>商品分类</h4> <ul class="multi-column-dropdown"> <li><a class="list" href="booktypes_list.action?pageNumber=1&btid=-1">全部系列</a></li> <c:forEach items="${bookTypes}" var="t"> <li><a class="list" href="booktypes_list.action?pageNumber=1&btid=${t.btid}">${t.btname}</a></li> </c:forEach> </ul> </div> </div> </li> </ul> </li> <li><a href="recommend_books.action?rtype=2&pageNumber=1" <c:if test="${param.flag==3 && t==2}">class="active"</c:if>>热销</a></li> <li><a href="recommend_books.action?rtype=3&pageNumber=1" <c:if test="${param.flag==3 && t==3}">class="active"</c:if>>新品</a></li> <c:choose> <c:when test="${empty user }"> <li><a href="user_register.jsp" <c:if test="${param.flag==10 }">class="active"</c:if>>注册</a></li> <li><a href="user_login.jsp" <c:if test="${param.flag==9 }">class="active"</c:if>>登录</a></li> </c:when> <c:otherwise> <li><a href="order_list.action" <c:if test="${param.flag==5 }">class="active"</c:if>>我的订单</a></li> <li><a href="user_center.jsp" <c:if test="${param.flag==4 }">class="active"</c:if>>个人中心</a></li> <li><a href="logout.action" >退出</a></li> <li><a href="admin/index.jsp" target="_blank">后台管理</a></li> </c:otherwise> </c:choose> </ul> <!--/.navbar-collapse--> </div> <!--//navbar-header--> </nav> <div class="header-info"> <div class="header-right search-box"> <a href="javascript:;"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></a> <div class="search"> <form class="navbar-form" action="search_books.action"> <input type="hidden" name="pageNumber" value="1"/> <input type="text" class="form-control" name="keyword"> <button type="submit" class="btn btn-default <c:if test="${param.flag==7 }">active</c:if>" aria-label="Left Align">搜索</button> </form> </div> </div> <div class="header-right cart"> <a href="book_cart.jsp"> <span class="glyphicon glyphicon-shopping-cart <c:if test="${param.flag==8 }">active</c:if>" aria-hidden="true"><span class="card_num"><c:choose><c:when test="${empty order}">0</c:when><c:otherwise>${order.itemMap.size()}</c:otherwise></c:choose></span></span> </a> </div> <div class="clearfix"> </div> </div> <div class="clearfix"> </div> </div> </div> <!--//header-- 修改代码使后台管理标签能跳转到后台管理页面
06-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值