54

I'm setting up a publish to a relative path for testing locally (especially xml config transformations). Each developer has their own path to the checked out project and I'd like to set up a publish that is machine/environment agnostic.

The publish dialog doesn't hint at any variables or wildcards that are allowed in it and it doesn't accept obj\publish or file://./obj/publish

Is there a way to publish to a relative filesystem path?

Maslow
  • 18,464
  • 20
  • 106
  • 193
  • 3
    I need to solve this problem too. Starting a bounty for it! – marzapower Jul 11 '11 at 11:55
  • Um why do you need to publish for development in the first place? And if this is for deployment most people use a build script in nant, msbuild, etc. – eaglestorm Jul 18 '11 at 10:27
  • @eaglestorm - to test the deploy functionality locally – Maslow Jul 26 '11 at 13:38
  • I would still use nant, msbuild or rake and add a deployment target to the build script – eaglestorm Jul 27 '11 at 13:02
  • 2
    @eaglestorm - also you can publish to a local folder and then collect the files into an installer package. This is kind of necessary when working with some installer products. – StingyJack Oct 09 '12 at 14:29
  • VS 2015 accepts something like ..\..\..\DeployFiles. The folder will be created if it does not already exist – wingyip Jul 27 '16 at 19:11

6 Answers6

49

For those using Visual Studio 2012 and the new publish configuration file (Properties/PublishProfiles/Local.pubxml) you can use this syntax in the file itself:

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <SiteUrlToLaunchAfterPublish />
    <publishUrl>$(MSBuildThisFileDirectory)..\..\obj\publish</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
  </PropertyGroup>
</Project>

Be aware that the $(MSBuildThisFileDirectory) property will resolve to the Local.pubxml directory. From there you should traverse up to get where you need to be.

Michiel van Oosterhout
  • 22,839
  • 15
  • 90
  • 132
  • As of the latest build, if you are doing this from the command line, you'll need to flip that around like this: $(MSBuildProjectDirectory)\..\..\obj\publish. That'll work in both VS2012 and from MSBuild. – joshua.ewer Feb 21 '13 at 19:22
  • The issue for VS2012 was fixed in [VS Update 2](http://www.microsoft.com/en-us/download/details.aspx?id=38188) (see also comments on [this answer](http://stackoverflow.com/questions/12024512/web-deploy-using-relative-paths-for-local-file-system-deployment/12029021#12029021)) – Trisped Apr 04 '13 at 20:52
  • 1
    Is the _issue for VS2012_ that got fixed the fact that it resolved to the publish profile directory instead of the actual project directory? Because you no longer need to jump up with `..\..` – drzaus Apr 10 '13 at 19:32
  • 1
    According to http://stackoverflow.com/a/12029021/61989 we should use $(MSBuildThisFileDirectory), this will always resolve to the PublishProfile folder – Patrick McDonald Jun 19 '14 at 14:50
  • You have one too many \ in there. No backslash after MSBuildThisFileDirectory.. e.g. $(MSBuildThisFileDirectory)..\..\obj\publish – LarryBud Mar 23 '17 at 17:27
  • Unfortunately, this works in a "one shot" way, because Visual Studio replaces the relative path based on this variable by an absolute path. I was intending to use this so that multple developpers with different file system structures could always publish to the same directory relatively to the project itself, but this won't do the trick. – Virus721 Jun 22 '23 at 09:31
18

Edit

For Visual Studio 2012, this solution will not work. You should look at the answer by michielvoo just after this one.

Original answer

After trying and trying I found the solution: you can use a relative path by setting

file:///./obj/publish

in the Publish WebApp dialog, but you must use a path that is already existent and writable by the user you are using. This way you can publish your web app in a local folder, or a path related folder.

Let me know if this helps. It did for me.

Community
  • 1
  • 1
marzapower
  • 5,531
  • 7
  • 38
  • 76
7

Just a regular path with forward slashes works in Visual Studio 2012:

../../../../../app

This starts at the published project's root directory.

Dima Korobskiy
  • 1,479
  • 16
  • 26
5

This worked for me in Visual Studio 2013:

file:\\..\..\..\Publish

Please note that this doesn't publish to obj\publish, as the original poster wanted, but to another directory (a few folders up) on my system as I desired. Modify it for obj\publish if you wish.

Dean
  • 4,554
  • 7
  • 34
  • 45
5

VS 2015 accepts something like

..\..\..\DeployFiles

It will also create the folder if it is missing so your publish settings can go into source control but you can easily ignore the DeployFiles folder and its contents.

wingyip
  • 3,465
  • 2
  • 34
  • 52
0

For the case where an additional post publish step needs to call msdeploy (MSVS 2015, dnx) with a relative path, another alternative is to edit project file (not the pubxml although that may work too) and create a variable that is the conversion of the relative path into an absolute path.

<Target Name="AfterWebPublish" AfterTargets="WebPublish">
     <!-- 
          msdeploy cannot currently handle relative paths for contentPath so first convert it to an absolute path 
     -->    
    <PropertyGroup>
        <AbsOutDir>$([System.IO.Path]::GetFullPath("$(ProjectDir)$(OutDir)"))</AbsOutDir>
    </PropertyGroup>

    <Exec WorkingDirectory="$(ProjectDir)" Command='call "$(DevEnvDir)..\Tools\vsvars32.bat"' />    
    <Exec WorkingDirectory="$(ProjectDir)" Command='"C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy" -verb:sync -source:contentPath="$(AbsOutDir)PublishOutput" -dest:package="$(AbsOutDir)$(MSBuildProjectName).zip"' />
  </Target>

Then $(AbsOutDir) can be used elsewhere as needed (such as for msdeploy contentPath). I don't think it can be entered within the dialog.

From "how-can-i-get-msbuild-to-evaluate-and-print-the-full-path-when-given-a-relative".

Community
  • 1
  • 1
crokusek
  • 5,345
  • 3
  • 43
  • 61